Esempio n. 1
0
        private void CreateRuntimeComponents()
        {
            SplineRuntimeEditor runtimeEditor = SplineRuntimeEditor.Instance;

            if (runtimeEditor != null)
            {
                m_renderer = GetComponent <MeshRenderer>();
                if (!m_renderer)
                {
                    m_renderer = gameObject.AddComponent <MeshRenderer>();
                }

                MeshFilter filter = GetComponent <MeshFilter>();
                if (!filter)
                {
                    filter = gameObject.AddComponent <MeshFilter>();
                }

                if (!filter.sharedMesh)
                {
                    filter.sharedMesh = runtimeEditor.ControlPointMesh;
                    UpdateMaterial();
                }

                if (!gameObject.GetComponent <ExposeToEditor>())
                {
                    gameObject.AddComponent <ExposeToEditor>();
                }
            }
        }
Esempio n. 2
0
        private void UpdateMaterial()
        {
            if (m_renderer != null)
            {
                SplineRuntimeEditor runtimeEditor = SplineRuntimeEditor.Instance;
                if (runtimeEditor != null)
                {
                    if (m_index % 3 == 0)
                    {
                        m_renderer.sharedMaterial = runtimeEditor.NormalMaterial;
                    }
                    else
                    {
                        if (m_index >= m_spline.ControlPointCount)
                        {
                            return;
                        }

                        ControlPointMode mode = m_spline.GetControlPointMode(m_index);
                        if (mode == ControlPointMode.Mirrored)
                        {
                            m_renderer.sharedMaterial = runtimeEditor.MirroredModeMaterial;
                        }
                        else if (mode == ControlPointMode.Aligned)
                        {
                            m_renderer.sharedMaterial = runtimeEditor.AlignedModeMaterial;
                        }
                        else
                        {
                            m_renderer.sharedMaterial = runtimeEditor.FreeModeMaterial;
                        }
                    }
                }
            }
        }
        public static void CreateRuntimeEditor(GameObject commandsPanel, string name)
        {
            if (!Object.FindObjectOfType <EventSystem>())
            {
                GameObject es = new GameObject();
                es.AddComponent <EventSystem>();
                es.AddComponent <StandaloneInputModule>();
                es.name = "EventSystem";
            }

            GameObject go = new GameObject();

            go.name = name;
            SplineRuntimeEditor srtEditor = go.AddComponent <SplineRuntimeEditor>();

            GameObject uiEditorGO = RTEditorMenu.InstantiateRuntimeEditor();

            uiEditorGO.transform.SetParent(go.transform, false);

            RuntimeEditor rtEditor = uiEditorGO.GetComponent <RuntimeEditor>();

            UnityEventTools.AddPersistentListener(rtEditor.Closed, new UnityAction(srtEditor.OnClosed));


            Placeholder[] placeholders = uiEditorGO.GetComponentsInChildren <Placeholder>(true);
            Placeholder   cmd          = placeholders.Where(p => p.Id == Placeholder.CommandsPlaceholder).First();

            commandsPanel.transform.SetParent(cmd.transform, false);

            Undo.RegisterCreatedObjectUndo(go, "Battlehub.Spline.CreateRuntimeEditor");
        }
Esempio n. 4
0
        private void LateUpdate()
        {
            if (Application.isPlaying)
            {
                if (SelectedSpline != null)
                {
                    if (Input.GetMouseButtonDown(0) && (EventSystem.current == null || !EventSystem.current.IsPointerOverGameObject()))
                    {
                        int selectedIndex = HitTest();
                        if (selectedIndex != -1)
                        {
                            SplineControlPoint ctrlPoint = SelectedSpline.GetComponentsInChildren <SplineControlPoint>().Where(p => p.Index == selectedIndex).FirstOrDefault();
                            RuntimeSelection.activeGameObject = ctrlPoint.gameObject;
                        }
                        else
                        {
                            if (RuntimeTools.Current != RuntimeTool.View)
                            {
                                if (RuntimeSelection.activeGameObject != null)
                                {
                                    if (PositionHandle.Current != null && !PositionHandle.Current.IsDragging)
                                    {
                                        if (SelectedSpline != null)
                                        {
                                            RuntimeSelection.activeGameObject = SelectedSpline.gameObject;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (m_instance == null)
            {
                m_instance = this;
                SplineBase[] splines = FindObjectsOfType <SplineBase>();
                for (int i = 0; i < splines.Length; ++i)
                {
                    SplineBase spline = splines[i];
                    if (spline.IsSelected)
                    {
                        spline.Select();
                    }
                }
            }
        }
Esempio n. 5
0
 private void LateUpdate()
 {
     if (m_instance == null)
     {
         m_instance = this;
         SplineBase[] splines = FindObjectsOfType <SplineBase>();
         for (int i = 0; i < splines.Length; ++i)
         {
             SplineBase spline = splines[i];
             if (spline.IsSelected)
             {
                 spline.Select();
             }
         }
     }
 }
Esempio n. 6
0
        private void DestroyRuntimeComponents()
        {
            SplineRuntimeEditor runtimeEditor = SplineRuntimeEditor.Instance;

            if (runtimeEditor != null)
            {
                MeshRenderer renderer = GetComponent <MeshRenderer>();
                if (renderer)
                {
                    DestroyImmediate(renderer);
                }

                MeshFilter filter = GetComponent <MeshFilter>();
                if (filter)
                {
                    DestroyImmediate(filter);
                }
            }
        }
Esempio n. 7
0
        private void OnDestroy()
        {
            if (!Application.isPlaying)
            {
                DisableRuntimeEditing();
            }

            if (Destroyed != null)
            {
                Destroyed(this, EventArgs.Empty);
            }

            if (m_hierarchy != null)
            {
                m_hierarchy.SelectionChanged -= OnHierarchySelectionChanged;
            }


            m_instance = null;
        }
Esempio n. 8
0
        private void OnDestroy()
        {
            if (!Application.isPlaying)
            {
                DisableRuntimeEditing();
            }

            bool enteringPlayMode = false;

            #if UNITY_EDITOR
            enteringPlayMode = UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode && !UnityEditor.EditorApplication.isPlaying;
            #endif


            if (!m_isApplicationQuit && !enteringPlayMode)
            {
                SplineControlPoint[] controlPoints = Resources.FindObjectsOfTypeAll <SplineControlPoint>();
                for (int i = 0; i < controlPoints.Length; ++i)
                {
                    SplineControlPoint controlPoint = controlPoints[i];
                    if (controlPoint != null)
                    {
                        controlPoint.DestroyRuntimeComponents();
                    }
                }
            }

            if (Destroyed != null)
            {
                Destroyed(this, EventArgs.Empty);
            }

            RuntimeSelection.SelectionChanged -= OnRuntimeSelectionChanged;

            m_instance = null;
        }
Esempio n. 9
0
 public void OnEnable()
 {
     m_instance = (SplineRuntimeEditor)target;
     m_fields   = ExposeProperties.GetProperties(m_instance);
 }
Esempio n. 10
0
        private void Awake()
        {
            #if UNITY_EDITOR
            UnityEditor.Selection.activeObject = null;
            #endif

            if (Camera == null)
            {
                Camera = Camera.main;
                if (Camera.main == null)
                {
                    Debug.LogError("Add Camera with MainCamera Tag");
                }
            }

            if (m_instance != null)
            {
                Debug.LogWarning("Another instance of SplineEditorSettings already exist");
            }

            if (m_mirroredModeMaterial == null)
            {
                Shader shader = Shader.Find("Battlehub/SplineEditor/SSBillboard");

                m_mirroredModeMaterial       = new Material(shader);
                m_mirroredModeMaterial.name  = "MirroredModeMaterial";
                m_mirroredModeMaterial.color = MirroredModeColor;
                m_mirroredModeMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
                m_mirroredModeMaterial.SetInt("_ZWrite", 1);
                m_mirroredModeMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
            }

            if (m_alignedModeMaterial == null)
            {
                m_alignedModeMaterial       = Instantiate(m_mirroredModeMaterial);
                m_alignedModeMaterial.name  = "AlignedModeMaterial";
                m_alignedModeMaterial.color = AlignedModeColor;
            }

            if (m_freeModeMaterial == null)
            {
                m_freeModeMaterial       = Instantiate(m_mirroredModeMaterial);
                m_freeModeMaterial.name  = "FreeModeMaterial";
                m_freeModeMaterial.color = FreeModeColor;
            }

            if (m_normalMaterial == null)
            {
                m_normalMaterial       = Instantiate(m_mirroredModeMaterial);
                m_normalMaterial.name  = "SplineMaterial";
                m_normalMaterial.color = Color.green;
            }

            if (m_connectedMaterial == null)
            {
                m_connectedMaterial       = Instantiate(m_mirroredModeMaterial);
                m_connectedMaterial.name  = "BranchMaterial";
                m_connectedMaterial.color = new Color32(0xa5, 0x00, 0xff, 0xff);
            }

            if (m_controlPointMesh == null)
            {
                m_controlPointMesh          = new Mesh();
                m_controlPointMesh.name     = "control point mesh";
                m_controlPointMesh.vertices = new[]
                {
                    new Vector3(0, 0, 0),
                    new Vector3(0, 0, 0),
                    new Vector3(0, 0, 0),
                    new Vector3(0, 0, 0)
                };
                m_controlPointMesh.triangles = new[]
                {
                    0, 1, 2, 0, 2, 3
                };
                m_controlPointMesh.uv = new[]
                {
                    new Vector2(-1, -1),
                    new Vector2(1, -1),
                    new Vector2(1, 1),
                    new Vector2(-1, 1)
                };
                m_controlPointMesh.RecalculateBounds();
            }

            m_instance = this;
            EnableRuntimeEditing();

            RuntimeSelection.SelectionChanged += OnRuntimeSelectionChanged;
        }
Esempio n. 11
0
        private void Awake()
        {
            if (m_instance != null)
            {
                Debug.LogWarning("Another instance of SplineEditorSettings already exist");
            }

            if (m_mirroredModeMaterial == null)
            {
                Shader shader = Shader.Find("Battlehub/SplineEditor/SSBillboard");

                m_mirroredModeMaterial       = new Material(shader);
                m_mirroredModeMaterial.name  = "MirroredModeMaterial";
                m_mirroredModeMaterial.color = MirroredModeColor;
                m_mirroredModeMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
                m_mirroredModeMaterial.SetInt("_ZWrite", 1);
                m_mirroredModeMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);
            }

            if (m_alignedModeMaterial == null)
            {
                m_alignedModeMaterial       = Instantiate(m_mirroredModeMaterial);
                m_alignedModeMaterial.name  = "AlignedModeMaterial";
                m_alignedModeMaterial.color = AlignedModeColor;
            }

            if (m_freeModeMaterial == null)
            {
                m_freeModeMaterial       = Instantiate(m_mirroredModeMaterial);
                m_freeModeMaterial.name  = "FreeModeMaterial";
                m_freeModeMaterial.color = FreeModeColor;
            }

            if (m_normalMaterial == null)
            {
                m_normalMaterial       = Instantiate(m_mirroredModeMaterial);
                m_normalMaterial.name  = "SplineMaterial";
                m_normalMaterial.color = SplineColor;
            }

            if (m_splineMaterial == null)
            {
                Shader shader = Shader.Find("Battlehub/SplineEditor/Spline");
                m_splineMaterial       = new Material(shader);
                m_splineMaterial.name  = "SplineMaterial";
                m_splineMaterial.color = SplineColor;
            }

            if (m_controlPointMesh == null)
            {
                m_controlPointMesh          = new Mesh();
                m_controlPointMesh.name     = "control point mesh";
                m_controlPointMesh.vertices = new[]
                {
                    new Vector3(0, 0, 0),
                    new Vector3(0, 0, 0),
                    new Vector3(0, 0, 0),
                    new Vector3(0, 0, 0)
                };
                m_controlPointMesh.triangles = new[]
                {
                    0, 1, 2, 0, 2, 3
                };
                m_controlPointMesh.uv = new[]
                {
                    new Vector2(-1, -1),
                    new Vector2(1, -1),
                    new Vector2(1, 1),
                    new Vector2(-1, 1)
                };
                m_controlPointMesh.RecalculateBounds();
            }

            m_instance = this;
            EnableRuntimeEditing();

            m_hierarchy = GetComponentInChildren <RuntimeHierarchy>();
            if (m_hierarchy != null)
            {
                m_hierarchy.TypeCriteria      = typeof(SplineBase);
                m_hierarchy.SelectionChanged += OnHierarchySelectionChanged;

#if UNITY_EDITOR
                m_hierarchy.SelectedItem = UnityEditor.Selection.activeGameObject;
#endif
            }
        }
Esempio n. 12
0
        public void Draw()
        {
            if (m_points.Length < 2)
            {
                return;
            }

            SplineRuntimeEditor runtimeEditor = SplineRuntimeEditor.Instance;

            if (runtimeEditor == null)
            {
                return;
            }
            runtimeEditor.SplineMaterial.SetPass(0);

            GL.PushMatrix();
            GL.MultMatrix(transform.localToWorldMatrix);
            GL.Begin(GL.LINES);
            Vector3 p0 = m_points[0];

            for (int i = 1; i < m_points.Length; i += 3)
            {
                Vector3 p1 = m_points[i];
                Vector3 p2 = m_points[i + 1];
                Vector3 p3 = m_points[i + 2];

                GL.Color(SplineRuntimeEditor.ControlPointLineColor);
                GL.Vertex(p0);
                GL.Vertex(p1);

                GL.Color(SplineRuntimeEditor.ControlPointLineColor);
                GL.Vertex(p2);
                GL.Vertex(p3);

                p0 = p3;
            }
            GL.End();
            GL.Begin(GL.LINES);
            GL.Color(SplineRuntimeEditor.SplineColor);
            p0 = m_points[0];
            for (int i = 1; i < m_points.Length; i += 3)
            {
                Vector3 p1 = m_points[i];
                Vector3 p2 = m_points[i + 1];
                Vector3 p3 = m_points[i + 2];

                float len   = (p0 - p1).magnitude + (p1 - p2).magnitude + (p2 - p3).magnitude;
                int   count = Mathf.CeilToInt(runtimeEditor.Smoothness * len);
                if (count <= 0)
                {
                    count = 1;
                }


                for (int j = 0; j < count; ++j)
                {
                    float   t     = ((float)j) / count;
                    Vector3 point = CurveUtils.GetPoint(p0, p1, p2, p3, t);

                    GL.Vertex(point);

                    t     = ((float)j + 1) / count;
                    point = CurveUtils.GetPoint(p0, p1, p2, p3, t);

                    GL.Vertex(point);
                }

                p0 = p3;
            }
            GL.End();
            GL.PopMatrix();
        }
Esempio n. 13
0
        private void SyncCtrlPoints(bool silent = false)
        {
            SplineRuntimeEditor runtimeEditor = SplineRuntimeEditor.Instance;

            SplineControlPoint[] ctrlPoints = GetControlPoints();
            int delta = ControlPointCount - ctrlPoints.Length;

            if (delta > 0)
            {
                for (int i = 0; i < delta; ++i)
                {
                    GameObject ctrlPoint = new GameObject();
                    ctrlPoint.SetActive(m_isSelected);
                    ctrlPoint.transform.parent     = transform;
                    ctrlPoint.transform.rotation   = Quaternion.identity;
                    ctrlPoint.transform.localScale = Vector3.one;

                    if (runtimeEditor != null)
                    {
                        MeshRenderer renderer = ctrlPoint.AddComponent <MeshRenderer>();
                        MeshFilter   filter   = ctrlPoint.AddComponent <MeshFilter>();
                        filter.sharedMesh       = runtimeEditor.ControlPointMesh;
                        renderer.sharedMaterial = runtimeEditor.NormalMaterial;
                        renderer.enabled        = true;// settings.RuntimeEditing;
                    }

                    ctrlPoint.name = "ctrl point";
                    AddControlPointComponent(ctrlPoint);

#if UNITY_EDITOR
                    if (!silent)
                    {
                        Undo.RegisterCreatedObjectUndo(ctrlPoint, Undo.GetCurrentGroupName());
                        EditorUtility.SetDirty(ctrlPoint);
                    }
#endif
                }

                ctrlPoints = GetControlPoints();
            }
            else if (delta < 0)
            {
                delta = -delta;
#if UNITY_EDITOR
                if (Application.isPlaying)
                {
                    for (int i = 0; i < delta; ++i)
                    {
                        SplineControlPoint ctrlPoint = ctrlPoints[i];
                        if (ctrlPoint.gameObject != null)
                        {
                            DestroyImmediate(ctrlPoint.gameObject);
                        }
                    }
                    ctrlPoints = GetControlPoints();
                }
                else
                {
                    if (silent)
                    {
                        for (int i = 0; i < delta; ++i)
                        {
                            SplineControlPoint ctrlPoint = ctrlPoints[i];
                            if (ctrlPoint.gameObject != null)
                            {
                                DestroyImmediate(ctrlPoint.gameObject);
                            }
                        }
                        ctrlPoints = GetControlPoints();
                    }
                    else
                    {
                        List <SplineControlPoint> ctrlPointsList = new List <SplineControlPoint>(ctrlPoints);
                        m_controlPointsToRemove = new SplineControlPoint[delta];
                        int skip = 0;
                        for (int i = 0; i < delta; ++i)
                        {
                            if (Selection.activeGameObject == ctrlPoints[i].gameObject)
                            {
                                delta++;
                                skip++;
                                continue;
                            }
                            m_controlPointsToRemove[i - skip] = ctrlPoints[i];
                            ctrlPointsList.Remove(ctrlPoints[i]);
                        }
                        if (m_removeCallback == null)
                        {
                            m_removeCallback = new EditorApplication.CallbackFunction(RemoveControlPoints);
                        }
                        EditorApplication.delayCall += m_removeCallback;
                        ctrlPoints = ctrlPointsList.ToArray();
                    }
                }
#else
                for (int i = 0; i < delta; ++i)
                {
                    SplineControlPoint ctrlPoint = ctrlPoints[i];
                    if (ctrlPoint.gameObject != null)
                    {
                        GameObject.DestroyImmediate(ctrlPoint.gameObject);
                    }
                }
                ctrlPoints = GetComponentsInChildren <SplineControlPoint>(true);
#endif
            }


            for (int i = 0; i < ControlPointCount; ++i)
            {
                SplineControlPoint ctrlPoint = ctrlPoints[i];
                ctrlPoint.Index = i;
                RaiseControlPointChanged(i);
                RaiseControlPointModeChanged(i);
            }
        }