public static void Synchronize(this HittTemplate template, Transform root, bool recursive = true) { var r = template.GetItemOf(root); if (r == null || r.entrances.Length == 0) { return; } // attempt to syncronize itself if (root.parent) { var pp = root.parent; var p = template.GetItemOf(pp); if (p != null && r.port.tag != 0) { var i = template.GetChildren(pp).IndexOf(root); var e = r.port; var g = p.entrances[i]; root.localRotation = HittUtility.Conjugate(e.rotation * g.rotation); root.localPosition = g.position + root.localRotation * -e.position; } } if (recursive) { for (int i = 0; i < root.childCount; i++) { template.Synchronize(root.GetChild(i), true); } } }
void MoveRight() { var p = template.GetChildren(activeObject.transform).ToArray(); if (p.Length > 0) { Selection.activeGameObject = p[HittUtility.Clamp(p, activeEntrance)].gameObject; } }
public static IEnumerable <Transform> GetChildren(this HittTemplate template, Transform parent, bool emptyasnull = true) { for (int i = 0; i < parent.childCount; i++) { var n = parent.GetChild(i); var g = HittUtility.GetPrefabOf(n.gameObject) as GameObject; if (g && template.objectIndex.GetValue(g.name) != null) { yield return(n); } else if (template.empty == g) { yield return(emptyasnull ? null : n); // for empty } } }
void InteractiveTemplate(SceneView view, Event ev) { if (active == null) { return; } if (ev.type == EventType.DragUpdated || ev.type == EventType.DragPerform) { var data = DragAndDrop.GetGenericData("MittEntrance") as GateTags; if (data != null) { dragTemplate = data; if (ev.type == EventType.DragUpdated) { float d; RaycastHit hit; Vector3 center = activeObject.transform.TransformPoint(active.center); Vector3 normal = -view.camera.transform.forward; if (!ev.shift) { normal = HittUtility.Align(normal); } Plane plane = new Plane(normal, center); Ray ray = HittUtility.MouseRay(ev, view); plane.Raycast(ray, out d); Vector3 point = ray.GetPoint(d); if (!ev.shift) { point = HittUtility.Snap(point - center, HandleUtility.GetHandleSize(center) * 0.2f) + center; } if (!(ev.control || ev.command) && Physics.Linecast(point, center, out hit)) { point = hit.point; } dragStop = new DragStop() { pos = point, center = center, normal = normal }; DragAndDrop.visualMode = DragAndDropVisualMode.Link; ev.Use(); } else { { Undo.RecordObject(activeObject, "Add new Entrance"); active.AddEntrance(new Entrance() { position = activeObject.transform.InverseTransformPoint(dragStop.pos), rotation = HittUtility.Conjugate(activeObject.transform.rotation) * Quaternion.LookRotation(dragStop.pos - dragStop.center), tag = dragTemplate.hash }); template.Populate(); } dragTemplate = null; DragAndDrop.activeControlID = 0; DragAndDrop.AcceptDrag(); ev.Use(); } } } else if (ev.type == EventType.DragExited) { dragTemplate = null; } if (ev.type == EventType.Repaint) { if (dragTemplate != null) { var r = Mathf.Min(Vector3.Magnitude(dragStop.pos - dragStop.center) * 0.5f, 1); var c = dragTemplate.color; Handles.color = c; Handles.DrawLine(dragStop.center, dragStop.pos); c.a *= 0.5f; Handles.color = c; Handles.DrawWireDisc(dragStop.center, dragStop.normal, r); c.a *= 0.2f; Handles.color = c; Handles.DrawSolidDisc(dragStop.center, dragStop.normal, r); } } }
public static HittItem GetItemOf(this HittTemplate template, GameObject g) { g = HittUtility.GetPrefabOf(g); return(g ? template.objectIndex.GetValue(g.name) : null); }
void EntranceGUI() { var ev = Event.current; var ms = ev.mousePosition; GateTags e = null; EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); if (template.gates.Length > 0) { e = template.gates[HittUtility.Clamp(template.gates, activeEntrance)]; e.name = EditorGUILayout.TextField(e.name); e.color = EditorGUILayout.ColorField(e.color); e.size = EditorGUILayout.Vector3Field(GUIContent.none, e.size); } if (GUILayout.Button("+", GUILayout.Width(50))) { Undo.RecordObject(template, "Add new Entrance Template"); ArrayUtility.Add(ref template.gates, e == null ? new GateTags() : e.Clone()); } if (GUILayout.Button("-", GUILayout.Width(50)) && template.gates.Length > 1) { Undo.RecordObject(template, "Delete Entrance Template"); ArrayUtility.RemoveAt(ref template.gates, activeEntrance); activeEntrance--; } if (EditorGUI.EndChangeCheck()) { SceneView.RepaintAll(); } EditorGUILayout.EndHorizontal(); Styles.MakeGrid(new Vector2(150, 50), template.gates.Length, delegate(Rect r, int i) { var g = template.gates[i]; var c = GUI.color; if (ev.type == EventType.Repaint) { GUI.color = g.color; GUI.Box(r, Texture2D.whiteTexture); GUI.color = c; GUI.Label(r, g.name, Styles.labelStyles[(Styles.IsDark(g.color) ? 1 : 0) + (activeEntrance == i ? 2 : 0)]); } if (ev.type == EventType.MouseDown && r.Contains(ms)) { activeEntrance = i; if (active != null) { DragAndDrop.PrepareStartDrag(); DragAndDrop.objectReferences = new UnityEngine.Object[] { template }; DragAndDrop.SetGenericData("MittEntrance", g); DragAndDrop.visualMode = DragAndDropVisualMode.Copy; DragAndDrop.StartDrag("Make Prefab"); } else { Repaint(); } } }); }
void ItemsGUI() { var ev = Event.current; var ms = ev.mousePosition; //if (activeItem >= 0) //{ // var e = template.items[activeItem]; // var ei = e ? e.GetComponent<HittItem>() : null; // if (!ei) // { // EditorGUILayout.HelpBox("Selected item does not have HittItem.", MessageType.None); // } // else // { // EditorGUILayout.BeginHorizontal(); // EditorGUILayout.LabelField(e.name); // bool exist = Array.IndexOf( template.items.con // EditorGUI.BeginDisabledGroup() // if (GUILayout.Button("+", GUILayout.Width(50))) // { // Undo.RecordObject(template, "Add new Entrance Template"); // ArrayUtility.Add(ref template.entrances, e.Clone()); // } // if (GUILayout.Button("-", GUILayout.Width(50)) && template.entrances.Length > 1) // { // Undo.RecordObject(template, "Delete Entrance Template"); // ArrayUtility.RemoveAt(ref template.entrances, activeEntrance); // activeEntrance--; // } // EditorGUILayout.EndHorizontal(); // } //} EditorGUILayout.BeginHorizontal(); scroll = EditorGUILayout.BeginScrollView(scroll, GUILayout.ExpandHeight(true)); Styles.MakeGrid(new Vector2(100, 100), template.items.Length, delegate(Rect r, int i) { var g = template.items[i]; if ((r).Contains(ms + scroll)) { if (ev.type == EventType.DragPerform || ev.type == EventType.DragUpdated) { var obj = HittUtility.GetPrefabOf(DragAndDrop.objectReferences.FirstOrDefault() as GameObject); if (obj && !g.prefab && !template.objectIndex.ContainsKey(obj.name)) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; if (ev.type == EventType.DragPerform) { g.prefab = obj; template.Populate(); DragAndDrop.AcceptDrag(); } ev.Use(); } } if (ev.type == EventType.MouseDown) { activeItem = i; if (g.prefab) { if (ev.clickCount >= 2) { EditorGUIUtility.PingObject(g.prefab); } else { DragAndDrop.PrepareStartDrag(); DragAndDrop.objectReferences = new UnityEngine.Object[] { g.prefab }; DragAndDrop.visualMode = DragAndDropVisualMode.Copy; DragAndDrop.StartDrag("Prefab Out"); } } } } if (ev.type == EventType.Repaint) { if (g.prefab) { GUI.Label(r, new GUIContent(AssetPreview.GetAssetPreview(g.prefab), g.name), Styles.labelThumb); if (activeItem == i) { var r2 = r; r2.height = 8; GUI.Box(r2, EditorGUIUtility.whiteTexture); } GUI.Label(r, g.name, Styles.labelThumbName); if (g.key != KeyCode.None) { GUI.Label(r, g.key.ToString(), Styles.labelThumbKey); } if (g.port.tag == 0) { GUI.Label(r, "??", Styles.labelThumbQuestion); } } else { GUI.Box(r, Texture2D.whiteTexture); GUI.Label(r, "+", Styles.labelStyles[2]); } } }); EditorGUILayout.EndScrollView(); if (GUILayoutUtility.GetLastRect().Contains(ms)) { if (ev.type == EventType.DragPerform || ev.type == EventType.DragUpdated) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (ev.type == EventType.DragPerform) { foreach (var o in DragAndDrop.objectReferences) { var g = HittUtility.GetPrefabOf(o as GameObject); if (template.GetItemOf(g) == null) { var i = new HittItem(); i.AssignPrefab(g); ArrayUtility.Add(ref template.items, i); } } template.Populate(); } } } EditorGUILayout.BeginVertical(GUILayout.Width(200), GUILayout.ExpandHeight(true)); { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("+")) { ArrayUtility.Add(ref template.items, new HittItem()); } if (GUILayout.Button("-")) { } EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); if (activeItem >= 0) { var g = template.items[activeItem]; g.name = EditorGUILayout.DelayedTextField(g.name); g.key = (KeyCode)EditorGUILayout.EnumPopup(g.key); if (GUILayout.Button("Reset Gates")) { g.port.tag = 0; g.entrances = new Entrance[] { }; SceneView.RepaintAll(); } if (GUILayout.Button("Crop Gates")) { var n = activeObject.GetComponentInChildren <MeshFilter>(); if (n && n.sharedMesh) { var b = n.sharedMesh.bounds; g.port.position = b.ClosestPoint(g.port.position); foreach (var e in g.entrances) { e.position = b.ClosestPoint(e.position); } SceneView.RepaintAll(); } } } } EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); }