Skip to content

Commit 5b12d13

Browse files
authored
Merge pull request #12 from DevSlem/release-3
Release 3.1.0
2 parents f986127 + 59c2db8 commit 5b12d13

File tree

9 files changed

+178
-40
lines changed

9 files changed

+178
-40
lines changed

Editor/MoveToolEditor.cs

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ namespace DevSlem.UnityEditor
1515
[CustomEditor(typeof(MonoBehaviour), false)]
1616
public class MoveToolEditor : Editor
1717
{
18-
private readonly GUIStyle style = new GUIStyle();
18+
private static readonly GUIStyle style = new GUIStyle();
19+
20+
private readonly static Vector3[] axisVector = new Vector3[3]
21+
{
22+
Vector3.right,
23+
Vector3.up,
24+
Vector3.forward
25+
};
1926

2027
public void OnEnable()
2128
{
@@ -221,11 +228,34 @@ private void SetVectorField(object obj, FieldInfo field, string label, MoveToolP
221228
// Pattern matching to test the fieldValue to see if it matches a vector type.
222229
switch (fieldValue)
223230
{
231+
case float oldFloat:
232+
field.SetValue(obj, SetPositionHandle(label, origin.x, oldFloat, obj, field, target));
233+
break;
224234
case Vector3 oldVector3:
225-
field.SetValue(obj, SetPositionHandle(label, origin, oldVector3, obj, field));
235+
field.SetValue(obj, SetPositionHandle(label, origin, oldVector3, obj, field, target));
226236
break;
227237
case Vector2 oldVector2:
228-
field.SetValue(obj, SetPositionHandle(label, (Vector2)origin, oldVector2, obj, field));
238+
field.SetValue(obj, SetPositionHandle(label, (Vector2)origin, oldVector2, obj, field, target));
239+
break;
240+
case IList<float> floatList:
241+
for (int i = 0; i < floatList.Count; i++)
242+
{
243+
string temp = label;
244+
if (!string.IsNullOrEmpty(label))
245+
temp += $" [{i}]";
246+
247+
float oldValue = floatList[i];
248+
float newValue = SetPositionHandle(temp, origin.x, oldValue, obj, field, target);
249+
//SetHandleVector3(temp, origin, oldValue, obj, field, v => list[i] = v);
250+
if (shiftClicked && newValue != oldValue)
251+
{
252+
float delta = newValue - oldValue;
253+
for (int j = 0; j < floatList.Count; j++)
254+
floatList[j] += delta;
255+
break;
256+
}
257+
floatList[i] = newValue;
258+
}
229259
break;
230260
case IList<Vector3> vector3List:
231261
for (int i = 0; i < vector3List.Count; i++)
@@ -235,7 +265,7 @@ private void SetVectorField(object obj, FieldInfo field, string label, MoveToolP
235265
temp += $" [{i}]";
236266

237267
Vector3 oldValue = vector3List[i];
238-
Vector3 newValue = SetPositionHandle(temp, origin, oldValue, obj, field);
268+
Vector3 newValue = SetPositionHandle(temp, origin, oldValue, obj, field, target);
239269
//SetHandleVector3(temp, origin, oldValue, obj, field, v => list[i] = v);
240270
if (shiftClicked && newValue != oldValue)
241271
{
@@ -255,7 +285,7 @@ private void SetVectorField(object obj, FieldInfo field, string label, MoveToolP
255285
temp += $" [{i}]";
256286

257287
Vector2 oldValue = vector2List[i];
258-
Vector2 newValue = SetPositionHandle(temp, (Vector2)origin, oldValue, obj, field);
288+
Vector2 newValue = SetPositionHandle(temp, (Vector2)origin, oldValue, obj, field, target);
259289
//SetHandleVector2(temp, origin, oldValue, obj, field, v => list[i] = v);
260290
if (shiftClicked && newValue != oldValue)
261291
{
@@ -426,12 +456,12 @@ private void SetVectorField(object obj, FieldInfo field, string label, MoveToolP
426456
/// Create a position handle for the vector3 oldValue. If it's changed, record the taget.
427457
/// </summary>
428458
/// <returns>changed vector3</returns>
429-
private Vector3 SetPositionHandle(string label, Vector3 origin, Vector3 oldValue, object obj, FieldInfo field)
459+
public static Vector3 SetPositionHandle(string label, Vector3 origin, Vector3 oldValue, object obj, FieldInfo field, UnityEngine.Object target = null)
430460
{
431461
Handles.Label(origin + oldValue, label, style);
432462
EditorGUI.BeginChangeCheck();
433463
Vector3 newValue = Handles.PositionHandle(origin + oldValue, Quaternion.identity) - origin;
434-
if (EditorGUI.EndChangeCheck())
464+
if (EditorGUI.EndChangeCheck() && target != null)
435465
{
436466
// enable ctrl + z & set dirty
437467
Undo.RecordObject(target, $"{target.name}_{target.GetInstanceID()}_{obj.GetHashCode()}_{field.Name}");
@@ -447,12 +477,48 @@ private Vector3 SetPositionHandle(string label, Vector3 origin, Vector3 oldValue
447477
/// Create Position Handle for Vector2. If it's changed, record the target.
448478
/// </summary>
449479
/// <returns>changed vector2</returns>
450-
private Vector2 SetPositionHandle(string label, Vector2 origin, Vector2 oldValue, object obj, FieldInfo field)
480+
public static Vector2 SetPositionHandle(string label, Vector2 origin, Vector2 oldValue, object obj, FieldInfo field, UnityEngine.Object target = null)
451481
{
452-
Handles.Label(origin + oldValue, label, style);
482+
//Handles.Label(origin + oldValue, label, style);
483+
//EditorGUI.BeginChangeCheck();
484+
//Vector2 newValue = (Vector2)Handles.PositionHandle(origin + oldValue, Quaternion.identity) - origin;
485+
//if (EditorGUI.EndChangeCheck())
486+
//{
487+
// // enable ctrl + z & set dirty
488+
// Undo.RecordObject(target, $"{target.name}_{target.GetInstanceID()}_{obj.GetHashCode()}_{field.Name}");
489+
490+
// // In the unity document, if the object may be part of a Prefab instance, we have to call this method.
491+
// // But, even if i don't call this method, it works well. I don't know the reason.
492+
// PrefabUtility.RecordPrefabInstancePropertyModifications(target);
493+
//}
494+
//return newValue;
495+
496+
Color[] axisColor = new Color[]
497+
{
498+
Handles.xAxisColor,
499+
Handles.yAxisColor,
500+
Handles.zAxisColor
501+
};
502+
503+
Vector3 position = origin + oldValue;
504+
Handles.Label(position, label, style);
453505
EditorGUI.BeginChangeCheck();
454-
Vector2 newValue = (Vector2)Handles.PositionHandle(origin + oldValue, Quaternion.identity) - origin;
455-
if (EditorGUI.EndChangeCheck())
506+
507+
Handles.color = Handles.zAxisColor;
508+
var size = HandleUtility.GetHandleSize(position) * 0.125f;
509+
var temp = Handles.Slider2D(position + new Vector3(1f, 1f) * size, Vector3.forward, axisVector[0], axisVector[1], size, Handles.RectangleHandleCap, new Vector2(EditorSnapSettings.move[0], EditorSnapSettings.move[1]))
510+
- new Vector3(1f, 1f) * size;
511+
position = (position - temp).sqrMagnitude > 0.001f ? temp : position;
512+
513+
for (int axis = 0; axis < 2; axis++)
514+
{
515+
Handles.color = axisColor[axis];
516+
Vector3 dir = axisVector[axis];
517+
position = Handles.Slider(position, dir, HandleUtility.GetHandleSize(position), Handles.ArrowHandleCap, EditorSnapSettings.move[axis])
518+
- (Vector3)origin;
519+
}
520+
521+
if (EditorGUI.EndChangeCheck() && target != null)
456522
{
457523
// enable ctrl + z & set dirty
458524
Undo.RecordObject(target, $"{target.name}_{target.GetInstanceID()}_{obj.GetHashCode()}_{field.Name}");
@@ -461,7 +527,27 @@ private Vector2 SetPositionHandle(string label, Vector2 origin, Vector2 oldValue
461527
// But, even if i don't call this method, it works well. I don't know the reason.
462528
PrefabUtility.RecordPrefabInstancePropertyModifications(target);
463529
}
464-
return newValue;
530+
return position;
531+
}
532+
533+
public static float SetPositionHandle(string label, float origin, float oldValue, object obj, FieldInfo field, UnityEngine.Object target = null)
534+
{
535+
var position = new Vector3(origin + oldValue, 0f, 0f);
536+
Handles.Label(position, label, style);
537+
EditorGUI.BeginChangeCheck();
538+
Handles.color = Handles.xAxisColor;
539+
Vector3 newValue = Handles.Slider(position, Vector3.right, HandleUtility.GetHandleSize(position), Handles.ArrowHandleCap, EditorSnapSettings.move.x)
540+
- new Vector3(origin, 0f, 0f);
541+
if (EditorGUI.EndChangeCheck() && target != null)
542+
{
543+
// enable ctrl + z & set dirty
544+
Undo.RecordObject(target, $"{target.name}_{target.GetInstanceID()}_{obj.GetHashCode()}_{field.Name}");
545+
546+
// In the unity document, if the object may be part of a Prefab instance, we have to call this method.
547+
// But, even if i don't call this method, it works well. I don't know the reason.
548+
PrefabUtility.RecordPrefabInstancePropertyModifications(target);
549+
}
550+
return newValue.x;
465551
}
466552

467553
/// <summary>
@@ -494,7 +580,7 @@ from f in fields
494580
/// <summary>
495581
/// Check wheter obj is vector type instance or vector type collection.
496582
/// </summary>
497-
private bool IsVector(object obj) => obj is Vector3 || obj is Vector2 || obj is IList<Vector3> || obj is IList<Vector2>;
583+
private bool IsVector(object obj) => obj is Vector3 || obj is Vector2 || obj is float || obj is IList<Vector3> || obj is IList<Vector2> || obj is IList<float>;
498584

499585

500586
/// <summary>

Images/move-tool-float.webp

1020 KB
Loading

Images/move-tool-float.webp.meta

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

Images/move-tool-vector2.webp

975 KB
Loading

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ It is recommended to use a latest, stable version.
1919
| Version | Release Date | Source | C# | .Net Compatibility |
2020
| :------------------------------------------------------------------------------------: | :----------: | :----------------------------------------------------------------------------: | :-----------: | :-------------------------: |
2121
| main(unstable) | -- | [main](https://github.com/DevSlem/unity-move-tool/tree/main) | 7.0 or higher | .Net Standard 2.0 or higher |
22-
| [Release 3.0.0](https://github.com/DevSlem/unity-move-tool/releases/tag/release-3.0.0) | 2022-06-22 | [release-3.0.0](https://github.com/DevSlem/unity-move-tool/tree/release-3.0.0) | 7.0 or higher | .Net standard 2.0 or higher |
22+
| [Release 3.1.0](https://github.com/DevSlem/unity-move-tool/releases/tag/release-3.1.0) | 2022-09-16 | [release-3.1.0](https://github.com/DevSlem/unity-move-tool/tree/release-3.1.0) | 7.0 or higher | .Net standard 2.0 or higher |
2323

2424
## Latest Update
2525

26-
* `KgmSlem` namespace is changed to `DevSlem`.
27-
* You can use Unity package system.
26+
* You can use **Move-Tool** for the `float` field.
27+
* Arrow of z direction of **Move-Tool** for the `Vector2` field has been removed. To be more intuitive!
2828

2929
## Installation
3030

3131
You can select 2 installation methods.
3232

3333
* Clone the repository and add `package.json` file to your project through Unity Package Manager.
34-
* You can add directly the package from git URL to your project through Unity Package Manager.
34+
* You can add directly the package from git URL to your project through Unity Package Manager. See the [Installing from a Git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html).
3535

3636
## Basic usage
3737

3838
You just define `MoveTool` attribute for a field for which you want to use position handle.
3939
The field is okay whether it's vector or vector collection.
40-
It works only if the type of the field is either `Vector3` or `Vector2`.
40+
It works only if the type of the field is one of the `Vector3`, `Vector2`, `float` type.
4141

4242
If you want to use ***attributes*** about move-tool, you must declare the following `using` directive.
4343

@@ -68,6 +68,16 @@ public class MoveToolSample : MonoBehaviour
6868
6969
![](/Images/move-tool-vector2.webp)
7070

71+
### Float
72+
73+
```c#
74+
[MoveTool] public float floatField;
75+
```
76+
77+
> Note that `float` type field only moves along the x axis.
78+
79+
![](/Images/move-tool-float.webp)
80+
7181
### Collection
7282

7383
You can use move-tool to `Array` or `List<T>` collection where each element is vector value.

Samples~/SamplesMoveTool/MoveTool Sample.prefab

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ MonoBehaviour:
4646
m_EditorClassIdentifier:
4747
vector: {x: 3, y: 0, z: 0}
4848
vector2: {x: 6, y: 0}
49+
floatField: 9
4950
vectorCollection:
50-
- {x: 9, y: 0, z: 0}
5151
- {x: 12, y: 0, z: 0}
52-
privateVector: {x: 15, y: 0, z: 0}
52+
- {x: 15, y: 0, z: 0}
53+
privateVector: {x: 18, y: 0, z: 0}
5354
privateCollection:
54-
- {x: 18, y: 0, z: 0}
5555
- {x: 21, y: 0, z: 0}
56-
customPropertyVector: {x: 24, y: 0, z: 0}
56+
- {x: 24, y: 0, z: 0}
57+
customPropertyVector: {x: 27, y: 0, z: 0}
5758
customClasses:
58-
- publicVector: {x: 27, y: 0, z: 0}
59-
serializedPrivateVector: {x: 30, y: 0, z: 0}
60-
- publicVector: {x: 33, y: 0, z: 0}
61-
serializedPrivateVector: {x: 36, y: 0, z: 0}
59+
- publicVector: {x: 30, y: 0, z: 0}
60+
serializedPrivateVector: {x: 33, y: 0, z: 0}
61+
- publicVector: {x: 36, y: 0, z: 0}
62+
serializedPrivateVector: {x: 39, y: 0, z: 0}

Samples~/SamplesMoveTool/MoveToolSample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class MoveToolSample : MonoBehaviour
77
{
88
[MoveTool] public Vector3 vector;
99
[MoveTool] public Vector2 vector2;
10+
[MoveTool] public float floatField;
1011
[MoveTool] public List<Vector3> vectorCollection = new List<Vector3>(); // Vector3[] array is also okay.
1112
[SerializeField, MoveTool] private Vector3 privateVector;
1213
[SerializeField, MoveTool] private List<Vector3> privateCollection = new List<Vector3>();

Samples~/SamplesMoveTool/MoveToolSampleScene.unity

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 705507994}
41-
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
41+
m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1}
4242
m_UseRadianceAmbientProbe: 0
4343
--- !u!157 &3
4444
LightmapSettings:
@@ -133,6 +133,7 @@ GameObject:
133133
m_Component:
134134
- component: {fileID: 705507995}
135135
- component: {fileID: 705507994}
136+
- component: {fileID: 705507996}
136137
m_Layer: 0
137138
m_Name: Directional Light
138139
m_TagString: Untagged
@@ -217,6 +218,26 @@ Transform:
217218
m_Father: {fileID: 0}
218219
m_RootOrder: 1
219220
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
221+
--- !u!114 &705507996
222+
MonoBehaviour:
223+
m_ObjectHideFlags: 0
224+
m_CorrespondingSourceObject: {fileID: 0}
225+
m_PrefabInstance: {fileID: 0}
226+
m_PrefabAsset: {fileID: 0}
227+
m_GameObject: {fileID: 705507993}
228+
m_Enabled: 1
229+
m_EditorHideFlags: 0
230+
m_Script: {fileID: 11500000, guid: 474bcb49853aa07438625e644c072ee6, type: 3}
231+
m_Name:
232+
m_EditorClassIdentifier:
233+
m_Version: 1
234+
m_UsePipelineSettings: 1
235+
m_AdditionalLightsShadowResolutionTier: 2
236+
m_LightLayerMask: 1
237+
m_CustomShadowLayers: 0
238+
m_ShadowLayerMask: 1
239+
m_LightCookieSize: {x: 1, y: 1}
240+
m_LightCookieOffset: {x: 0, y: 0}
220241
--- !u!1 &963194225
221242
GameObject:
222243
m_ObjectHideFlags: 0
@@ -308,51 +329,63 @@ PrefabInstance:
308329
m_Modification:
309330
m_TransformParent: {fileID: 0}
310331
m_Modifications:
311-
- target: {fileID: 8872950196097312925, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
332+
- target: {fileID: 8872950196097312925, guid: 15615e270044b4b4bae1016dc8ac08f0,
333+
type: 3}
312334
propertyPath: m_Name
313335
value: MoveTool Sample
314336
objectReference: {fileID: 0}
315-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
337+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
338+
type: 3}
316339
propertyPath: m_RootOrder
317340
value: 2
318341
objectReference: {fileID: 0}
319-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
342+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
343+
type: 3}
320344
propertyPath: m_LocalPosition.x
321345
value: 0
322346
objectReference: {fileID: 0}
323-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
347+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
348+
type: 3}
324349
propertyPath: m_LocalPosition.y
325350
value: 0
326351
objectReference: {fileID: 0}
327-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
352+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
353+
type: 3}
328354
propertyPath: m_LocalPosition.z
329355
value: 0
330356
objectReference: {fileID: 0}
331-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
357+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
358+
type: 3}
332359
propertyPath: m_LocalRotation.w
333360
value: 1
334361
objectReference: {fileID: 0}
335-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
362+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
363+
type: 3}
336364
propertyPath: m_LocalRotation.x
337365
value: 0
338366
objectReference: {fileID: 0}
339-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
367+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
368+
type: 3}
340369
propertyPath: m_LocalRotation.y
341370
value: 0
342371
objectReference: {fileID: 0}
343-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
372+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
373+
type: 3}
344374
propertyPath: m_LocalRotation.z
345375
value: 0
346376
objectReference: {fileID: 0}
347-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
377+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
378+
type: 3}
348379
propertyPath: m_LocalEulerAnglesHint.x
349380
value: 0
350381
objectReference: {fileID: 0}
351-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
382+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
383+
type: 3}
352384
propertyPath: m_LocalEulerAnglesHint.y
353385
value: 0
354386
objectReference: {fileID: 0}
355-
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0, type: 3}
387+
- target: {fileID: 8872950196097312926, guid: 15615e270044b4b4bae1016dc8ac08f0,
388+
type: 3}
356389
propertyPath: m_LocalEulerAnglesHint.z
357390
value: 0
358391
objectReference: {fileID: 0}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.devslem.movetool",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"displayName": "Move Tool",
55
"description": "You can use position handles of fields for which you define MoveToolAttribute in the unity editor scene view.",
66
"unity": "2020.3",

0 commit comments

Comments
 (0)