I’m attempting to attract an icon utilizing GUI.DrawTexture at a world-space place iconPos to signify a sun icon.
It labored, however I’m dealing with two points:
1- Two icons are being drawn — one on the sun’s place and the opposite in the other way — and I have no idea why the icon in the other way is being drawn.
2- The second situation is that after I regulate the icon’s dimension, it begins to jitter (the dimensions will increase from one aspect of the icon however not from the opposite till the following increment), or I’m not precisely positive what is going on. In consequence, the icon doesn’t find yourself within the appropriate place or the proper 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 Shade IconColor = Shade.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't 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.shade = _drawer.IconColor;
GUI.DrawTexture(rect, _drawer.Icon);
Handles.EndGUI();
}
static void StopDrawing()
{
if (!_isDrawing)
return;
_isDrawing = false;
SceneView.duringSceneGui -= DrawSunIcon;
SceneView.RepaintAll();
}
}
