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>();
                }
            }
        }
        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");
        }
 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();
             }
         }
     }
 }
        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;
        }
        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;
        }