Beispiel #1
0
        /// <summary>
        /// Unity's 'OnEnable' method. Handles some initialization.
        /// </summary>
        void OnEnable()
        {
            // If Start hasn't been called yet, then this has been called too early.
            // Start() will call this when it is time
            if (!started)
            {
                return;
            }

            //_enabled = true;

            if (objectLayer != -1)
            {
                ClearObjectLayerFromCameras();
                ClearObjectLayerFromLights();
            }

            UIObject3DTimer.AtEndOfFrame(() => UpdateDisplay(true), this);

#if UNITY_EDITOR
#if UNITY_2017_2_OR_NEWER
            EditorApplication.playModeStateChanged += InEditorCleanup;
#else
            EditorApplication.playmodeStateChanged += InEditorCleanup;
#endif
#endif
        }
Beispiel #2
0
        /// <summary>
        /// Call this method if you've updated your target object (e.g. a model in your scene)
        /// and you want UIObject3D's copy to be updated to match.
        /// Performs a small cleanup and then updates and schedules a render.
        /// (Has less of a performance hit than HardUpdateDisplay())
        /// </summary>
        public void RefreshTarget()
        {
            if (_target != null)
            {
                Cleanup();
            }

            UIObject3DTimer.AtEndOfFrame(() => UpdateDisplay(), this);
        }
Beispiel #3
0
        void InEditorCleanup()
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
            {
                Cleanup();

                UIObject3DTimer.AtEndOfFrame(() =>
                {
                    Cleanup();
                }, this, true);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Clear all textures/etc. destroy the current target objects, and then start from scratch.
        /// Necessary, if, for example, the RectTransform size has changed.
        /// Fairly performance-intensive - only call this if strictly necessary.
        /// </summary>
        public void HardUpdateDisplay()
        {
            var color = imageComponent.color;

            if (Application.isPlaying)
            {
                imageComponent.color = new Color(0, 0, 0, 0);
                //imageComponent.sprite = null;
            }

            DestroyResources();

            Cleanup();

            //UpdateDisplay();
            UIObject3DTimer.AtEndOfFrame(() => UpdateDisplay(), this);
            UIObject3DTimer.DelayedCall(0.05f, () => { imageComponent.color = color; }, this, true);
        }
        public override void OnInspectorGUI()
        {
            Dictionary <UIObject3D, Transform> targetPrefabs = new Dictionary <UIObject3D, Transform>();

            targetPrefabs = targets.ToDictionary(k => k as UIObject3D, v => (v as UIObject3D).ObjectPrefab);
            Dictionary <UIObject3D, float> renderScales = targets.ToDictionary(k => k as UIObject3D, v => (v as UIObject3D).RenderScale);

            EditorGUI.BeginChangeCheck();

            if (GUILayout.Button("Force Render"))
            {
                foreach (var t in targetPrefabs)
                {
                    t.Key.HardUpdateDisplay();
                }
            }

            EditorGUILayout.Space();

            base.OnInspectorGUI();

            if (!EditorGUI.EndChangeCheck())
            {
                return;
            }

            foreach (var t in targetPrefabs)
            {
                if (t.Key.ObjectPrefab != t.Value ||
                    renderScales[t.Key] != t.Key.RenderScale)
                {
                    t.Key.SetStarted();
                    t.Key.HardUpdateDisplay();
                    UIObject3DTimer.AtEndOfFrame(() => t.Key.UpdateDisplay(), t.Key);
                }
                else
                {
                    t.Key.UpdateDisplay(true);
                }
            }
        }
Beispiel #6
0
        void ScheduleRender()
        {
            if (UIObject3D == null || !enabled)
            {
                return;
            }

            if (!Application.isPlaying)
            {
                UIObject3DTimer.AtEndOfFrame(() =>
                {
                    UIObject3D.OnUpdateTarget.RemoveListener(UpdateLightEvent);
                    UIObject3D.UpdateDisplay();
                    UIObject3D.OnUpdateTarget.AddListener(UpdateLightEvent);
                }, this);
            }
            else
            {
                UIObject3D.Render();
            }
        }
Beispiel #7
0
        private static void NewUIObject3D()
        {
            Transform parent = UnityEditor.Selection.activeTransform;

            if (parent == null || !(parent is RectTransform))
            {
                parent = GetCanvasTransform();
            }

            var prefabGUID = AssetDatabase.FindAssets("UIObject3D t:GameObject").FirstOrDefault();

            if (prefabGUID != null)
            {
                var prefab = (GameObject)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(prefabGUID), typeof(GameObject));

                var newUIObject3D = GameObject.Instantiate(prefab);

                newUIObject3D.name = "UIObject3D";
                newUIObject3D.transform.SetParent(parent);

                var transform = newUIObject3D.transform as RectTransform;

                transform.localPosition      = Vector3.zero;
                transform.anchoredPosition3D = Vector3.zero;
                transform.localScale         = Vector3.one;
                transform.localRotation      = Quaternion.identity;

                transform.SetParent(parent);

                transform.anchorMin = Vector2.zero;
                transform.anchorMax = Vector2.one;
                transform.offsetMin = Vector2.zero;
                transform.offsetMax = Vector2.zero;

                var uiObject3D = newUIObject3D.GetComponent <UIObject3D>();

                UIObject3DTimer.DelayedCall(0.01f, () => uiObject3D.HardUpdateDisplay(), uiObject3D);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Unity's Start() method. Used for initialization.
        /// </summary>
        void Start()
        {
            var color = imageComponent.color;

            if (Application.isPlaying)
            {
                imageComponent.color = new Color(0, 0, 0, 0);
                //imageComponent.sprite = null;
            }

            UIObject3DTimer.AtEndOfFrame(() => SetStarted(), this, true);
            UIObject3DTimer.AtEndOfFrame(() => OnEnable(), this);

            // Some models (particularly, models with rigs) can cause Unity to crash if they are instantiated this early (for some reason)
            // as such, we must delay very briefly to avoid this before rendering
            UIObject3DTimer.DelayedCall(0.01f, () =>
            {
                Cleanup();
                UpdateDisplay();

                UIObject3DTimer.DelayedCall(0.05f, () => { imageComponent.color = color; }, this, true);
            }, this, true);
        }