Skip to content

Commit

Permalink
Use custom sprite for ship markers
Browse files Browse the repository at this point in the history
  • Loading branch information
schlangguru committed Aug 15, 2021
1 parent ea40a01 commit 8aaba7c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
23 changes: 21 additions & 2 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.IO;
using BepInEx;
using BepInEx;
using HarmonyLib;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityEngine.Rendering;


namespace VH_Ship_Marker_Mod
Expand All @@ -21,15 +24,31 @@ public class Main : BaseUnityPlugin
};
public static Dictionary<ZDOID, ShipMarkerData> ShipMarkers = new Dictionary<ZDOID, ShipMarkerData>();

public static Sprite ShipMarkerSprite;


void Awake()
{
ShipMarkerSprite = LoadShipMarkerSprite();
_harmony = new Harmony("VH_Ship_Marker_Mod");
_harmony.PatchAll(typeof(ZDOMan_Patch));
_harmony.PatchAll(typeof(Minimap_Patch));
}


public Sprite LoadShipMarkerSprite()
{
Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("VH_Ship_Marker_Mod.Resources.mapicon_anchor.png");
byte[] buffer = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(buffer, 0, (int)manifestResourceStream.Length);
// Create a texture. Texture size does not matter, since LoadImage will replace with with incoming image size.
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(buffer);
texture.Apply();

return Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.0f, 0.0f), 50f);
}

private void OnDestroy()
{
this._harmony?.UnpatchSelf();
Expand Down
7 changes: 1 addition & 6 deletions MiniMap_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static void DrawShipMarker(ShipMarkerData data, float size, RectTransfor

gameObject = UnityEngine.Object.Instantiate<GameObject>(Minimap.instance.m_pinPrefab);
data.Marker = gameObject;
gameObject.GetComponent<Image>().sprite = GetShipMarkerSprite();
gameObject.GetComponent<Image>().sprite = Main.ShipMarkerSprite;
gameObject.transform.SetParent(parent);
(gameObject.transform as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size);
(gameObject.transform as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size);
Expand All @@ -89,11 +89,6 @@ private static void DrawShipMarker(ShipMarkerData data, float size, RectTransfor
}
}

private static Sprite GetShipMarkerSprite()
{
return Minimap.instance.m_icons.Find((Minimap.SpriteData x) => x.m_name == Minimap.PinType.Icon2).m_icon;
}

private static bool IsPointVisible(Vector3 p, RawImage map)
{
float num;
Expand Down
Binary file added Resources/mapicon_anchor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion valheim-ship-marker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<ManagedDataPath>$(GamePath)\valheim_Data\Managed</ManagedDataPath>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\mapicon_anchor.png" />
</ItemGroup>

<ItemGroup>
<Reference Include="BepinEx">
<HintPath>$(BepInExPath)\core\BepInEx.dll</HintPath>
Expand All @@ -22,11 +26,14 @@
<HintPath>$(ManagedDataPath)\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>$(ManagedDataPath)\UnityEngine.CoreModule.dll</HintPath>
<HintPath>$(GamePath)\unstripped_corlib\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI">
<HintPath>$(ManagedDataPath)\UnityEngine.UI.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.ImageConversionModule">
<HintPath>$(GamePath)\unstripped_corlib\UnityEngine.ImageConversionModule.dll</HintPath>
</Reference>
<Reference Include="Valheim">
<HintPath>$(ManagedDataPath)\assembly_valheim.dll</HintPath>
</Reference>
Expand Down

0 comments on commit 8aaba7c

Please sign in to comment.