I’m making an attempt to attract an icon utilizing GUI.DrawTexture at a world-space place iconPos to symbolize a sun icon.
It labored, however after I modify the icon’s dimension, it begins to jitter (the scale will increase from one aspect of the icon however not from the opposite till the subsequent increment), or I’m not precisely positive what is occurring. In consequence, the icon doesn’t find yourself within the appropriate place or the right form — I can’t decide it exactly.
Script Code:
utilizing UnityEngine;
public class SunIconDrawer : MonoBehaviour
{
public float sunAltitude, sunAzimuth;
public Texture2D Icon;
public float IconSize = 10f;
public Coloration IconColor = Coloration.white;
}
Editor Script Code:
utilizing System.Drawing;
utilizing UnityEditor;
utilizing UnityEngine;
[CustomEditor(typeof(SunIconDrawer))]
public class SunIconDrawerEditor : Editor
{
static SunIconDrawer _drawer;
static bool _isDrawing = false;
[InitializeOnLoadMethod]
static void ResetDrawingOnReload()
{
StopDrawing();
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
_drawer = (SunIconDrawer)goal;
GUILayout.House(10);
if (GUILayout.Button("Draw Icon"))
{
if (_isDrawing)
return;
_isDrawing = true;
SceneView.duringSceneGui += DrawSunIcon;
SceneView.RepaintAll();
}
if (GUILayout.Button("Cease Drawing"))
{
StopDrawing();
}
}
static void DrawSunIcon(SceneView sceneView)
{
if(_drawer == null || _drawer.Icon == null)
{
Debug.LogWarning("Can not draw sun icon: Lacking SunIconDrawer, SunTransform, or Icon.");
StopDrawing();
return;
}
Remodel cam = sceneView.digital camera.remodel;
Vector3 cameraPos = cam.place;
Quaternion sunRotation = Quaternion.Euler(_drawer.sunAltitude, _drawer.sunAzimuth, 0);
Vector3 sunDir = sunRotation * -Vector3.ahead;
Vector3 iconPos = cameraPos + sunDir;
Vector3 guiPoint = HandleUtility.WorldToGUIPoint(iconPos);
float scale = HandleUtility.GetHandleSize(iconPos);
float pixelSize = _drawer.IconSize / scale;
Rect rect = new Rect(
guiPoint.x - pixelSize * 0.5f,
guiPoint.y - pixelSize * 0.5f,
pixelSize,
pixelSize
);
Handles.BeginGUI();
GUI.colour = _drawer.IconColor;
GUI.DrawTexture(rect, _drawer.Icon);
Handles.EndGUI();
}
static void StopDrawing()
{
if (!_isDrawing)
return;
_isDrawing = false;
SceneView.duringSceneGui -= DrawSunIcon;
SceneView.RepaintAll();
}
}
