Skip to content

Commit ac4dbd3

Browse files
committed
update
add arrows
1 parent 9dcedef commit ac4dbd3

File tree

11 files changed

+246
-18
lines changed

11 files changed

+246
-18
lines changed

Editor/NoteArrowEditor.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#if UNITY_EDITOR
2+
using System.Collections.Generic;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace DCFApixels.Notes.Editors
7+
{
8+
[CustomEditor(typeof(NoteArrow))]
9+
[InitializeOnLoad]
10+
public class NoteArrowEditor : Editor
11+
{
12+
private const float arrowHeight = 0.0085f;
13+
[DrawGizmo(GizmoType.Active | GizmoType.NonSelected)]
14+
static void DrawGizmo(NoteArrow obj, GizmoType type)
15+
{
16+
if (obj.Target == null)
17+
return;
18+
19+
if(!_arrows.Contains(obj))
20+
_arrows.Add(obj);
21+
}
22+
private static HashSet<NoteArrow> _arrows = new HashSet<NoteArrow>();
23+
private static HashSet<NoteArrow> _removedArrows = new HashSet<NoteArrow>();
24+
static NoteArrowEditor()
25+
{
26+
SceneView.duringSceneGui += SceneView_duringSceneGui;
27+
}
28+
private static void SceneView_duringSceneGui(SceneView scene)
29+
{
30+
if (Event.current.type == EventType.Repaint)
31+
{
32+
Camera camera = scene.camera;
33+
bool isOrthographic = camera.orthographic;
34+
_removedArrows.Clear();
35+
foreach (var item in _arrows)
36+
{
37+
if (item == null || !item.gameObject.activeInHierarchy)
38+
_removedArrows.Add(item);
39+
}
40+
_arrows.SymmetricExceptWith(_removedArrows);
41+
foreach (var obj in _arrows)
42+
{
43+
if (obj.Target == null)
44+
return;
45+
46+
Color color = Color.white;
47+
if (obj.TryGetComponent(out INote inote))
48+
color = inote.Color;
49+
50+
Vector3 startPoint = obj.transform.position;
51+
Vector3 endPoint = obj.Target.position;
52+
53+
Color defaultColor = Handles.color;
54+
55+
Handles.color = color;
56+
57+
Vector3 toCameraDirection;
58+
if (isOrthographic)
59+
{
60+
Plane plane = new Plane(Vector3.up, startPoint);
61+
plane.SetNormalAndPosition(camera.transform.forward, camera.transform.position);
62+
var cp = plane.ClosestPointOnPlane(startPoint);
63+
float distacne = Vector3.Distance(cp, startPoint);
64+
toCameraDirection = -camera.transform.forward * distacne * 2f;
65+
}
66+
else
67+
{
68+
toCameraDirection = camera.transform.position - startPoint;
69+
}
70+
71+
float height = arrowHeight * toCameraDirection.magnitude;
72+
73+
float startOffset = 0.02f * toCameraDirection.magnitude;
74+
float endOffset = 0.2f;
75+
76+
Vector3 direction = endPoint - startPoint;
77+
Quaternion q = Quaternion.LookRotation(toCameraDirection, direction);
78+
79+
endPoint -= direction.normalized * endOffset;
80+
startPoint = startPoint + q * (Vector3.up * startOffset);
81+
82+
Vector3 startPoint1 = startPoint + q * (Vector3.left * height / 2f);
83+
Vector3 startPoint2 = startPoint + q * (Vector3.right * height / 2f);
84+
85+
Handles.DrawLine(startPoint, endPoint);
86+
Handles.DrawLine(startPoint1, endPoint);
87+
Handles.DrawLine(startPoint2, endPoint);
88+
89+
Handles.color = defaultColor;
90+
}
91+
}
92+
}
93+
94+
public override void OnInspectorGUI()
95+
{
96+
var targetProp = serializedObject.FindProperty("_target");
97+
98+
NoteArrow target = this.target as NoteArrow;
99+
Color color = Color.white;
100+
if (target.TryGetComponent(out INote inote))
101+
color = inote.Color;
102+
103+
Color defaultBackgroundColor = GUI.backgroundColor;
104+
GUI.backgroundColor = color;
105+
106+
Rect rect = new Rect(0, 0, EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing * 2 + 9);
107+
EditorGUI.DrawRect(rect, color);
108+
109+
float labelWidth = EditorGUIUtility.labelWidth;
110+
EditorGUIUtility.labelWidth = 0;
111+
112+
EditorGUI.BeginChangeCheck();
113+
EditorGUILayout.PropertyField(targetProp);
114+
if (EditorGUI.EndChangeCheck())
115+
serializedObject.ApplyModifiedProperties();
116+
117+
GUI.backgroundColor = defaultBackgroundColor;
118+
}
119+
}
120+
}
121+
#endif

Editor/NoteArrowEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/NoteEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public override void OnInspectorGUI()
170170
GUILayout.Box(_lineTex, GUILayout.Height(1), GUILayout.ExpandWidth(true));
171171

172172
EditorGUI.BeginChangeCheck();
173-
string newValue = EditorGUILayout.TextArea(textProp.hasMultipleDifferentValues? "-" : textProp.stringValue, areastyle, GUILayout.Height(heightProp.floatValue));
173+
string newValue = EditorGUILayout.TextArea(textProp.hasMultipleDifferentValues ? "-" : textProp.stringValue, areastyle, GUILayout.Height(heightProp.floatValue));
174174
if (EditorGUI.EndChangeCheck())
175175
{
176176
textProp.stringValue = newValue;

Editor/NoteUtility.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,65 @@ namespace DCFApixels.Notes.Editors
99
internal static class NoteUtility
1010
{
1111
private static string _gizmosPath;
12+
private static GameObject FindRoot(string name)
13+
{
14+
GameObject root = GameObject.Find(name);
15+
if (root == null)
16+
root = new GameObject(name);
17+
return root;
18+
}
1219

20+
#region CreateLazyNote
21+
[MenuItem("GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof(LazyNote) + "with arrow")]
22+
public static void CreateLazyNoteWithArrow(MenuCommand menuCommand)
23+
{
24+
GameObject go = CreateLazyNoteInternal(menuCommand);
25+
go.AddComponent<NoteArrow>();
26+
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
27+
}
1328
[MenuItem("GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof(LazyNote))]
1429
public static void CreateLazyNote(MenuCommand menuCommand)
30+
{
31+
GameObject go = CreateLazyNoteInternal(menuCommand);
32+
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
33+
}
34+
private static GameObject CreateLazyNoteInternal(MenuCommand menuCommand)
1535
{
1636
GameObject go = new GameObject(nameof(LazyNote));
1737
go.AddComponent<LazyNote>();
1838
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
19-
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
39+
if (go.transform.parent == null)
40+
go.transform.parent = FindRoot(NOTES_ROOT_NAME).transform;
2041
Selection.activeObject = go;
42+
return go;
43+
}
44+
#endregion
45+
46+
#region CreateNote
47+
[MenuItem("GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof(Note) + "with arrow")]
48+
public static void CreateNoteWithArrow(MenuCommand menuCommand)
49+
{
50+
GameObject go = CreateNoteInternal(menuCommand);
51+
go.AddComponent<NoteArrow>();
52+
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
2153
}
2254
[MenuItem("GameObject/" + ASSET_SHORT_NAME + "/Create " + nameof(Note))]
2355
public static void CreateNote(MenuCommand menuCommand)
56+
{
57+
GameObject go = CreateNoteInternal(menuCommand);
58+
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
59+
}
60+
private static GameObject CreateNoteInternal(MenuCommand menuCommand)
2461
{
2562
GameObject go = new GameObject(nameof(Note));
2663
go.AddComponent<Note>();
2764
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
28-
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
65+
if (go.transform.parent == null)
66+
go.transform.parent = FindRoot(NOTES_ROOT_NAME).transform;
2967
Selection.activeObject = go;
68+
return go;
3069
}
70+
#endregion
3171

3272
[DrawGizmo(GizmoType.Selected | GizmoType.NonSelected | GizmoType.Pickable)]
3373
private static void DrawLazyNote(LazyNote note, GizmoType gizmoType)
@@ -61,7 +101,7 @@ internal static string GetGizmosPath()
61101
string packagePath = null;
62102
if (assembly != null)
63103
packagePath = UnityEditor.PackageManager.PackageInfo.FindForAssembly(assembly)?.assetPath;
64-
if(string.IsNullOrEmpty(packagePath))
104+
if (string.IsNullOrEmpty(packagePath))
65105
packagePath = "Assets";
66106
_gizmosPath = packagePath + "/Gizmos";
67107

Editor/NotesSettings.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ public void Save()
8383
}
8484
Save(false);
8585
}
86-
//public void SetNewAuthors(IEnumerable<AuthorInfo> authors)
87-
//{
88-
// _authorsSerialization = authors.ToArray();
89-
//}
90-
//public void SetNewNoteTypes(IEnumerable<NoteTypeInfo> types)
91-
//{
92-
// _typesSerialization = types.ToArray();
93-
//}
86+
//public void SetNewAuthors(IEnumerable<AuthorInfo> authors)
87+
//{
88+
// _authorsSerialization = authors.ToArray();
89+
//}
90+
//public void SetNewNoteTypes(IEnumerable<NoteTypeInfo> types)
91+
//{
92+
// _typesSerialization = types.ToArray();
93+
//}
9494

9595
#region ISerializationCallbackReceiver
9696
[SerializeField]

Editor/NotesSettingsWindow.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal static void Open()
2222

2323
private void OnGUI()
2424
{
25-
if(target == null)
25+
if (target == null)
2626
{
2727
Settigns.hideFlags = ~HideFlags.HideAndDontSave;
2828
target = new SerializedObject(Settigns);
@@ -35,18 +35,18 @@ private void OnGUI()
3535
int oldAuthorsCount = authorsProp.arraySize;
3636
int oldTypesCount = typesProp.arraySize;
3737
GUI.enabled = true;
38-
38+
3939
EditorGUI.BeginChangeCheck();
4040
EditorGUILayout.PropertyField(authorsProp, new GUIContent("Authors"));
4141
EditorGUILayout.PropertyField(typesProp, new GUIContent("Types"));
4242
if (EditorGUI.EndChangeCheck())
4343
{
44-
if(authorsProp.arraySize != oldAuthorsCount)
44+
if (authorsProp.arraySize != oldAuthorsCount)
4545
{
4646
for (int i = oldAuthorsCount; i < authorsProp.arraySize; i++)
4747
authorsProp.GetArrayElementAtIndex(i).FindPropertyRelative("_id").intValue = 0;
4848
}
49-
if(typesProp.arraySize != oldTypesCount)
49+
if (typesProp.arraySize != oldTypesCount)
5050
{
5151
for (int i = oldTypesCount; i < typesProp.arraySize; i++)
5252
typesProp.GetArrayElementAtIndex(i).FindPropertyRelative("_id").intValue = 0;

Runtime/Consts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace DCFApixels.Notes
44
{
55
internal class NotesConsts
66
{
7+
public const string NOTES_ROOT_NAME = "NOTES";
8+
79
public const string ASSET_SHORT_NAME = "Notes";
810
public const string AUTHOR = "DCFApixels";
911
public const string NOTE_SEPARATOR = ">-<";

Runtime/LazyNote.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace DCFApixels.Notes
55
{
66
using static NotesConsts;
77
[AddComponentMenu(ASSET_SHORT_NAME + "/" + nameof(LazyNote), 30)]
8-
internal class LazyNote : MonoBehaviour
8+
internal class LazyNote : MonoBehaviour, INote
99
{
1010
#if UNITY_EDITOR
1111
[SerializeField]

Runtime/Note.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
namespace DCFApixels.Notes
88
{
99
using static NotesConsts;
10+
public interface INote
11+
{
12+
public string Text { get; }
13+
public Color Color { get; }
14+
}
1015
[AddComponentMenu(ASSET_SHORT_NAME + "/" + nameof(Note), 30)]
11-
internal class Note : MonoBehaviour
16+
internal class Note : MonoBehaviour, INote
1217
{
1318
#if UNITY_EDITOR
1419
[SerializeField]
@@ -106,6 +111,18 @@ public bool DrawIcon
106111
#endif
107112
}
108113
}
114+
115+
public Color Color
116+
{
117+
get
118+
{
119+
#if UNITY_EDITOR
120+
return _type.color;
121+
#else
122+
return default;
123+
#endif
124+
}
125+
}
109126
#endregion
110127
}
111128
}

Runtime/NoteArrow.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using UnityEngine;
2+
3+
namespace DCFApixels.Notes
4+
{
5+
using static NotesConsts;
6+
7+
[AddComponentMenu(ASSET_SHORT_NAME + "/" + nameof(NoteArrow), 30)]
8+
public class NoteArrow : MonoBehaviour
9+
{
10+
#if UNITY_EDITOR
11+
[SerializeField]
12+
private Transform _target;
13+
#endif
14+
public Transform Target
15+
{
16+
get
17+
{
18+
#if UNITY_EDITOR
19+
return _target;
20+
#else
21+
return null;
22+
#endif
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)