Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright (c) 2010 Bob Berkebile
- //
- // Modified by ~PeterSvP
- // Made initial points at the transform position and close to it,
- // added a [+Add] button - adds a point nesr the last one,
- // added Drop field where you can drop scene object to add its transform as a point,
- // added a [M] button that adds point in between its row and the next
- // added DELETE buttons for each point to be able to delete it
- //
- // Please direct any bugs/comments/suggestions to http://www.pixelplacement.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- using UnityEngine;
- using UnityEditor;
- using System.Collections;
- using System.Linq;
- [CustomEditor(typeof(iTweenPath))]
- public class iTweenPathEditor : Editor
- {
- iTweenPath _target;
- GUIStyle style = new GUIStyle();
- public static int count = 0;
- static GameObject drop;
- static string dropName = "\n\nDrop object to add its pos\n\n"; // the (Transform) is out of view and it's centered :D
- void OnEnable()
- {
- //i like bold handle labels since I'm getting old:
- style.fontStyle = FontStyle.Bold;
- style.normal.textColor = Color.white;
- _target = (iTweenPath)target;
- //lock in a default path name:
- if (!_target.initialized)
- {
- _target.initialized = true;
- _target.pathName = "New Path " + ++count;
- _target.initialName = _target.pathName;
- }
- }
- public override void OnInspectorGUI()
- {
- //draw the path?
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.PrefixLabel("Path Visible");
- _target.pathVisible = EditorGUILayout.Toggle(_target.pathVisible);
- EditorGUILayout.EndHorizontal();
- //path name:
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.PrefixLabel("Path Name");
- _target.pathName = EditorGUILayout.TextField(_target.pathName);
- EditorGUILayout.EndHorizontal();
- if (_target.pathName == "")
- {
- _target.pathName = _target.initialName;
- }
- //path color:
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.PrefixLabel("Path Color");
- _target.pathColor = EditorGUILayout.ColorField(_target.pathColor);
- EditorGUILayout.EndHorizontal();
- //exploration segment count control:
- EditorGUILayout.BeginHorizontal();
- //EditorGUILayout.PrefixLabel("Node Count");
- _target.nodeCount = Mathf.Max(2, EditorGUILayout.IntField("Node Count", _target.nodeCount));
- //_target.nodeCount = Mathf.Clamp(EditorGUILayout.IntSlider(_target.nodeCount, 0, 10), 2,100);
- EditorGUILayout.EndHorizontal();
- //add node?
- if (_target.nodeCount > _target.nodes.Count)
- {
- // if noone, add small tween from current to +1 forward instead of (0,0,0)
- if (_target.nodes.Count == 0)
- {
- _target.nodes.Add(_target.transform.position);
- _target.nodes.Add(_target.transform.position + Vector3.forward);
- }
- // Else add close to last
- else for (int i = 0; i < _target.nodeCount - _target.nodes.Count; i++)
- {
- int c = _target.nodes.Count;
- _target.nodes.Add(Vector3.LerpUnclamped(_target.nodes[c - 2], _target.nodes[c - 1], 1.5f));
- }
- }
- //remove node?
- if (_target.nodeCount < _target.nodes.Count)
- {
- if (EditorUtility.DisplayDialog("Remove path node?", "Shortening the node list will permantently destory parts of your path. This operation cannot be undone.", "OK", "Cancel"))
- {
- int removeCount = _target.nodes.Count - _target.nodeCount;
- _target.nodes.RemoveRange(_target.nodes.Count - removeCount, removeCount);
- }
- else {
- _target.nodeCount = _target.nodes.Count;
- }
- }
- //node display:
- bool disableDelete = _target.nodeCount <= 2;
- for (int i = 0; i < _target.nodes.Count; i++)
- {
- EditorGUILayout.BeginHorizontal();
- GUILayout.Label((i + 1) + ":", GUILayout.MaxWidth(20));
- _target.nodes[i] = EditorGUILayout.Vector3Field("", _target.nodes[i]);
- GUI.enabled = i < _target.nodes.Count - 1;
- if (GUILayout.Button("M", GUILayout.MaxWidth(20)))
- {
- Vector3 mid = (_target.nodes[i] + _target.nodes[i + 1]) / 2;
- _target.nodes.Insert(i+1, mid); _target.nodeCount++; return;
- }
- GUI.enabled = !disableDelete;
- if (GUILayout.Button("X", GUILayout.MaxWidth(20))) { _target.nodes.RemoveAt(i); _target.nodeCount--; return; }
- GUI.enabled = true;
- EditorGUILayout.EndHorizontal();
- }
- // Utility buttons feature - add new close to last or add from scene object
- EditorGUILayout.BeginHorizontal();
- if (GUILayout.Button("+ Add")) _target.nodeCount++;
- // Hack to dislay custom text into the ObjectField
- if (drop == null)
- {
- drop = new GameObject();
- drop.hideFlags = HideFlags.HideAndDontSave;
- drop.name = dropName;
- }
- // If something was selected, add its position
- Transform changed = EditorGUILayout.ObjectField(drop, typeof(Transform), true) as Transform;
- if (changed != null)
- {
- _target.nodeCount++;
- _target.nodes.Add(changed.position);
- }
- EditorGUILayout.EndHorizontal();
- // Update and redraw:
- if (GUI.changed)
- {
- EditorUtility.SetDirty(_target);
- }
- }
- void OnSceneGUI()
- {
- if (_target.pathVisible)
- {
- if (_target.nodes.Count > 0)
- {
- //allow path adjustment undo:
- Undo.SetSnapshotTarget(_target, "Adjust iTween Path");
- //path begin and end labels:
- Handles.Label(_target.nodes[0], "'" + _target.pathName + "' Begin", style);
- Handles.Label(_target.nodes[_target.nodes.Count - 1], "'" + _target.pathName + "' End", style);
- //node handle display:
- for (int i = 0; i < _target.nodes.Count; i++)
- {
- _target.nodes[i] = Handles.PositionHandle(_target.nodes[i], Quaternion.identity);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement