Skip to content

Commit dec6787

Browse files
committed
UnityX
1 parent b6a53d3 commit dec6787

File tree

28 files changed

+497
-78
lines changed

28 files changed

+497
-78
lines changed

Assets/UnityX/Scripts/Components/PolygonRenderer/Editor/PolygonOutlineRendererEditor.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public override void OnInspectorGUI() {
2626
serializedObject.ApplyModifiedProperties();
2727
}
2828

29-
void OnSceneGUI () {
29+
protected override void OnMultiEditSceneGUI () {
30+
base.OnMultiEditSceneGUI();
3031
Undo.RecordObject(data, "Edit polygon");
3132
polygonEditor.OnSceneGUI(data.polygon);
3233
}

Assets/UnityX/Scripts/Components/PolygonRenderer/Editor/PolygonRendererEditor.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public override void OnInspectorGUI() {
2626
serializedObject.ApplyModifiedProperties();
2727
}
2828

29-
void OnSceneGUI () {
29+
protected override void OnMultiEditSceneGUI () {
30+
base.OnMultiEditSceneGUI();
3031
Undo.RecordObject(data, "Edit polygon");
3132
polygonEditor.OnSceneGUI(data.polygon);
3233
}

Assets/UnityX/Scripts/Components/Transform/Transform Copier/Editor/TransformCopierEditor.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public override void OnEnable() {
1515
void OnDisable() {
1616
Tools.hidden = false;
1717
}
18-
19-
void OnSceneGUI () {
18+
protected override void OnMultiEditSceneGUI () {
2019
Tools.hidden = data.enabled && (Application.isPlaying && data.playMode || !Application.isPlaying && data.editMode) && ((data.position && Tools.current == Tool.Move) || (data.rotation && Tools.current == Tool.Rotate));
2120
var color = Handles.color;
2221
Handles.color = Color.blue;

Assets/UnityX/Scripts/Components/UI/WorldSpaceUIElement/WorldSpaceUIElement.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public Vector3 GetLocalScale () {
150150

151151
protected override void Awake () {
152152
#if UNITY_EDITOR
153-
if(!Application.isPlaying && !_updateInEditMode)
154-
return;
153+
if(UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(gameObject) != null) return;
154+
if(!Application.isPlaying && !_updateInEditMode) return;
155155
#endif
156156
rectTransform = this.GetRectTransform();
157157
if(camera == null)
@@ -161,16 +161,16 @@ protected override void Awake () {
161161

162162
protected override void OnEnable () {
163163
#if UNITY_EDITOR
164-
if(!Application.isPlaying && !_updateInEditMode)
165-
return;
164+
if(UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(gameObject) != null) return;
165+
if(!Application.isPlaying && !_updateInEditMode) return;
166166
#endif
167167
Refresh();
168168
}
169169

170170
protected override void OnTransformParentChanged () {
171171
#if UNITY_EDITOR
172-
if(!Application.isPlaying && !_updateInEditMode)
173-
return;
172+
if(UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(gameObject) != null) return;
173+
if(!Application.isPlaying && !_updateInEditMode) return;
174174
#endif
175175
SetRootCanvas();
176176
base.OnTransformParentChanged ();
@@ -179,8 +179,8 @@ protected override void OnTransformParentChanged () {
179179
// LateUpdate because we want it to come even after camera updates
180180
private void LateUpdate () {
181181
#if UNITY_EDITOR
182-
if(!Application.isPlaying && !_updateInEditMode)
183-
return;
182+
if(UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(gameObject) != null) return;
183+
if(!Application.isPlaying && !_updateInEditMode) return;
184184
#endif
185185
Refresh();
186186
}

Assets/UnityX/Scripts/Editor Tools/Base Editor Class/Editor/BaseEditor.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ public override void OnInspectorGUI() {
3333
if(GUI.changed && target != null) {
3434
EditorUtility.SetDirty(target);
3535
}
36-
37-
serializedObject.ApplyModifiedProperties();
3836
}
39-
37+
38+
public virtual void OnSceneGUI() {
39+
if (target == datas [0]) {
40+
OnMultiEditSceneGUI ();
41+
}
42+
}
43+
44+
// Called once per SceneView refresh. You should iterate the targets/datas array to draw per object here
45+
protected virtual void OnMultiEditSceneGUI () {}
46+
4047
protected void SetData () {
4148
// If an object has been deleted under our feet we need to handle it gracefully
4249
// (Previously it would assert)

Assets/UnityX/Scripts/Editor Tools/EditorTime/Editor/EditorTime.cs

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using UnityEditor;
33
using System.Collections;
44

5+
#if UNITY_EDITOR
56
[InitializeOnLoad]
7+
#endif
68
public class EditorTime {
79

810
public static float time {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
// A bunch of utilities for the scene view - useful for culling handles/gizmos for performance reasons.
6+
public static class SceneViewUtility {
7+
#if UNITY_EDITOR
8+
static bool ready;
9+
static Plane[] frustumPlanes = new Plane[6];
10+
static Vector3 sceneViewCameraPosition;
11+
static bool sceneViewCameraIsOrthographic;
12+
public static float sceneViewCameraOrthographicSize;
13+
14+
static float maxDistance = 750;
15+
static float minOrthographicSizeFraction = 0.035f;
16+
17+
static SceneViewUtility () {
18+
UnityEditor.SceneView.beforeSceneGui += BeforeSceneGUI;
19+
}
20+
21+
static void BeforeSceneGUI (UnityEditor.SceneView sceneView) {
22+
ready = true;
23+
GeometryUtility.CalculateFrustumPlanes(UnityEditor.SceneView.currentDrawingSceneView.camera, frustumPlanes);
24+
sceneViewCameraIsOrthographic = UnityEditor.SceneView.currentDrawingSceneView.camera.orthographic;
25+
sceneViewCameraOrthographicSize = UnityEditor.SceneView.currentDrawingSceneView.camera.orthographicSize;
26+
sceneViewCameraPosition = UnityEditor.SceneView.currentDrawingSceneView.camera.transform.position;
27+
}
28+
29+
public static bool IsVisibleInSceneView (Vector3 point) {
30+
return ready &&
31+
(sceneViewCameraIsOrthographic ? true : Vector3.Distance(sceneViewCameraPosition, point) < maxDistance)
32+
&&
33+
TestPlanesPoint(frustumPlanes, point);
34+
}
35+
public static bool IsVisibleInSceneView (Vector3 point, float radius) {
36+
return ready &&
37+
(sceneViewCameraIsOrthographic ? radius > sceneViewCameraOrthographicSize*minOrthographicSizeFraction : Vector3.Distance(sceneViewCameraPosition, point) < maxDistance)
38+
&&
39+
TestPlanesSphere(frustumPlanes, point, radius);
40+
}
41+
public static bool IsVisibleInSceneView (Bounds bounds) {
42+
return ready &&
43+
(sceneViewCameraIsOrthographic ? bounds.extents.magnitude > sceneViewCameraOrthographicSize*minOrthographicSizeFraction : Vector3.Distance(sceneViewCameraPosition, bounds.ClosestPoint(sceneViewCameraPosition)) < maxDistance)
44+
&&
45+
GeometryUtility.TestPlanesAABB(frustumPlanes, bounds);
46+
}
47+
48+
public static bool TestPlanesPoint (Plane[] planes, Vector3 point) {
49+
for(int i = 0; i < 6; i++)
50+
if(Vector3.Dot(planes[i].normal, point) + planes[i].distance < 0)
51+
return false;
52+
return true;
53+
}
54+
public static bool TestPlanesSphere (Plane[] planes, Vector3 point, float radius) {
55+
for(int i = 0; i < 6; i++)
56+
if(Vector3.Dot(planes[i].normal, point) + planes[i].distance + radius < 0)
57+
return false;
58+
return true;
59+
}
60+
#endif
61+
}

Assets/UnityX/Scripts/Editor Tools/SceneView/SceneViewUtility.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UnityX/Scripts/Extensions/Collections/ShuffleBag.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public ShuffleBag (ShuffleBag<T> otherBag) {
3131
_items = new List<T>(otherBag.items);
3232
}
3333

34-
private void RefreshBag () {
34+
private void RefreshBag (bool clearBeforeAdding = true) {
35+
if(clearBeforeAdding) _items.Clear();
3536
foreach(var item in _sourceItems) {
3637
_items.Add(item);
3738
}

Assets/UnityX/Scripts/Extensions/Geometry/Polygon/Polygon.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public bool GetIsClockwise () {
535535
return x >= 0;
536536
}
537537

538-
public void ReverseDirection () {
538+
public void FlipWindingOrder () {
539539
System.Array.Reverse(_vertices);
540540
}
541541

Assets/UnityX/Scripts/Extensions/Spline System/Editor/SplineEditor.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using UnityEngine;
1+
using UnityEngine;
52
using UnityEditor;
63

74
namespace SplineSystem {
@@ -227,7 +224,7 @@ private void DrawControlPointHandle (ref SplineBezierPoint bezierPoint, ref Spli
227224

228225
var newControlPointPosition = DrawEditableControlPointHandle(controlPointPosition, controlPointRotation);
229226
if(controlPointPosition != newControlPointPosition) {
230-
controlPoint.distance = Vector3.Dot(newControlPointPosition - bezierPointPosition, controlPointRotation * Vector3.forward);
227+
controlPoint.distance = Vector3.Dot(matrix.inverse.MultiplyPoint3x4(newControlPointPosition) - matrix.inverse.MultiplyPoint3x4(bezierPointPosition), controlPointRotation * Vector3.forward);
231228
changed = true;
232229
}
233230
}
@@ -306,9 +303,8 @@ SamplePoint GetSamplePoint (Spline spline, Vector2 mousePosition) {
306303
var lastPoint = HandleUtility.WorldToGUIPoint(matrix.MultiplyPoint3x4(curve._points[0]));
307304
for (var j = 1; j < curve._points.Length; j++) {
308305
var nextPoint = HandleUtility.WorldToGUIPoint(matrix.MultiplyPoint3x4(curve._points[j]));
309-
UnityX.Geometry.Line line = new UnityX.Geometry.Line(lastPoint, nextPoint);
310-
var normalizedDistanceOnLine = line.GetNormalizedDistanceOnLine(mousePosition);
311-
var pointOnLine = Vector2.LerpUnclamped(line.start, line.end, normalizedDistanceOnLine);
306+
var normalizedDistanceOnLine = GetNormalizedDistanceOnLine(lastPoint, nextPoint, mousePosition);
307+
var pointOnLine = Vector2.LerpUnclamped(lastPoint, nextPoint, normalizedDistanceOnLine);
312308
var sqrDistanceFromLine = (mousePosition - pointOnLine).sqrMagnitude;
313309
if(sqrDistanceFromLine < bestSqrDistance) {
314310
bestSqrDistance = sqrDistanceFromLine;
@@ -335,6 +331,16 @@ SamplePoint GetSamplePoint (Spline spline, Vector2 mousePosition) {
335331
}
336332
}
337333
return new SamplePoint(bestCurveIndex, bestCurvePointIndex, bestBezierPointIndex, bestNormalizedDistanceOnLine);
334+
335+
static float GetNormalizedDistanceOnLine(Vector2 start, Vector2 end, Vector2 p, bool clamped = true) {
336+
float sqrLength = (start.x-end.x) * (start.x-end.x) + (start.y-end.y) * (start.y-end.y);
337+
if (sqrLength == 0f) return 0;
338+
// Divide by length squared so that we can save on normalising (end-start), since
339+
// we're effectively dividing by the length an extra time.
340+
float n = Vector2.Dot(p - start, end - start) / sqrLength;
341+
if(!clamped) return n;
342+
return Mathf.Clamp01(n);
343+
}
338344
}
339345

340346
/*

Assets/UnityX/Scripts/Extensions/Spline System/Spline.cs

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using UnityEngine;
22
using System.Collections.Generic;
3-
using UnityX.Geometry;
43

54
namespace SplineSystem {
65
public delegate void OnSplineChangeEvent(Spline spline);
@@ -50,7 +49,10 @@ public float length {
5049
}
5150
}
5251
public Bounds bounds;
53-
52+
53+
public Spline () {
54+
Validate();
55+
}
5456
public Spline (params SplineBezierPoint[] bezierPoints) {
5557
this.bezierPoints = bezierPoints;
5658
RefreshCurveData();
@@ -148,7 +150,7 @@ public static Spline CreateFromPoints (IList<Vector3> points, IList<Quaternion>
148150
}
149151

150152
public void RefreshCurveData () {
151-
if(bezierPoints.Length <= 1) return;
153+
if(bezierPoints == null || bezierPoints.Length <= 1) return;
152154
if(curves == null || curves.Length != bezierPoints.Length-1) curves = new SplineBezierCurve[bezierPoints.Length-1];
153155
for (int i = 0; i < bezierPoints.Length-1; i++) curves[i] = new SplineBezierCurve(bezierPoints [i], bezierPoints [i+1]);
154156

@@ -392,8 +394,8 @@ float EstimateArcLengthAlongCurve (Vector3 position, SplineBezierCurve bestCurve
392394
var leftPoint = bestCurve.GetPointAtT(leftT);
393395
var rightPoint = bestCurve.GetPointAtT(rightT);
394396

395-
closestDistanceAlongLeftLine = Line3D.GetNormalizedDistanceOnLine(leftPoint, centerPoint, position, clampAtStart);
396-
closestDistanceAlongRightLine = Line3D.GetNormalizedDistanceOnLine(centerPoint, rightPoint, position, clampAtEnd);
397+
closestDistanceAlongLeftLine = GetNormalizedDistanceOnLine(leftPoint, centerPoint, position, clampAtStart);
398+
closestDistanceAlongRightLine = GetNormalizedDistanceOnLine(centerPoint, rightPoint, position, clampAtEnd);
397399
float closestDistanceOnLineA = SqrDistance(position, Vector3.LerpUnclamped(leftPoint, centerPoint, closestDistanceAlongLeftLine));
398400
float closestDistanceOnLineB = SqrDistance(position, Vector3.LerpUnclamped(centerPoint, rightPoint, closestDistanceAlongRightLine));
399401
var arcLengthOffsetMultiplier = 0f;
@@ -478,8 +480,8 @@ public bool Validate () {
478480
quality = defaultQuality;
479481
didChange = true;
480482
}
481-
if(bezierPoints.Length < 2) {
482-
this.bezierPoints = new SplineBezierPoint[] {
483+
if(bezierPoints == null || bezierPoints.Length < 2) {
484+
bezierPoints = new SplineBezierPoint[] {
483485
new SplineBezierPoint(new Vector3(-1,1,0), Quaternion.LookRotation(Vector3.right, Vector3.forward), 1f, 1f),
484486
new SplineBezierPoint(new Vector3(1,-1,0), Quaternion.LookRotation(Vector3.right, Vector3.forward), 1f, 1f)
485487
};
@@ -547,5 +549,19 @@ public static void DrawCurveLineGizmos (Spline spline, SplineBezierCurve curve,
547549
static float SqrDistance (Vector3 a, Vector3 b) {
548550
return (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y) + (a.z-b.z) * (a.z-b.z);
549551
}
552+
553+
static float GetNormalizedDistanceOnLine(Vector3 start, Vector3 end, Vector3 p, bool clamped = true) {
554+
float sqrLength = SqrDistance(start, end);
555+
return GetNormalizedDistanceOnLineInternal(start, end, p, sqrLength, clamped);
556+
}
557+
558+
static float GetNormalizedDistanceOnLineInternal(Vector3 start, Vector3 end, Vector3 p, float sqrLength, bool clamped = true) {
559+
if (sqrLength == 0f) return 0;
560+
// Divide by length squared so that we can save on normalising (end-start), since
561+
// we're effectively dividing by the length an extra time.
562+
float n = Vector3.Dot(p - start, end - start) / sqrLength;
563+
if(!clamped) return n;
564+
return Mathf.Clamp01(n);
565+
}
550566
}
551567
}

Assets/UnityX/Scripts/Extensions/Spline System/SplineBezierCurve.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using UnityEngine;
2-
using System.Linq;
3-
using System.Collections;
4-
using System.Collections.Generic;
5-
using UnityX.Geometry;
62

73
namespace SplineSystem {
84
// Arc length == NormalizedDistance

Assets/UnityX/Scripts/Extensions/UnityEditorX/ComponentMenuX.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void MoveToTop(Component component) {
1313
if(component.gameObject.GetComponent<Component>() == component) return;
1414
Component[] comps = component.gameObject.GetComponents<Component>();
1515
if(component == comps[0]) return;
16-
int componentIndex = comps.IndexOf(component);
16+
int componentIndex = System.Array.IndexOf(comps, component);
1717
for(int i = 0; i < componentIndex-1; i++)
1818
UnityEditorInternal.ComponentUtility.MoveComponentUp (component);
1919
}
@@ -22,7 +22,7 @@ public static void MoveToBottom(Component component) {
2222
if(component == null) return;
2323
Component[] comps = component.gameObject.GetComponents<Component>();
2424
if(component == comps[comps.Length-1]) return;
25-
int componentIndex = comps.IndexOf(component);
25+
int componentIndex = System.Array.IndexOf(comps, component);
2626
for(int i = 0; i < comps.Length-componentIndex-1; i++)
2727
UnityEditorInternal.ComponentUtility.MoveComponentDown (component);
2828
}

Assets/UnityX/Scripts/Extensions/UnityEditorX/Editor/SelectionX.cs

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
/// <summary>
88
/// Provides callbacks for more specific selection events.
99
/// </summary>
10+
#if UNITY_EDITOR
1011
[InitializeOnLoad]
12+
#endif
1113
public static class SelectionX {
1214
const string editorPrefsPath = "SelectionXLastSelection";
1315

0 commit comments

Comments
 (0)