예제 #1
0
        private void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty propertyViewAsset  = serializedProperty.GetArrayElementAtIndex(index);
            SerializedProperty propertyViewTypeID = propertyViewAsset.FindPropertyRelative("viewTypeID");
            SerializedProperty propertyAssetID    = propertyViewAsset.FindPropertyRelative("assetID");

            string viewName  = UViewEditorUtils.GetViewName(propertyViewTypeID);
            string assetPath = AssetDatabase.GUIDToAssetPath(propertyAssetID.stringValue);

            if (UViewEditorUtils.ValidateViewAsset(propertyViewAsset))
            {
                System.Type  viewType      = System.Type.GetType(propertyViewTypeID.stringValue);
                AbstractView sceneInstance = _loadedViews.ContainsKey(viewType) ? _loadedViews[viewType] : null;
                bool         existsInScene = sceneInstance != null;

                EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width, rect.height), existsInScene ? string.Format("{0} (Loaded)", viewName) : viewName, existsInScene ? EditorStyles.boldLabel : EditorStyles.label);

                if (existsInScene && GUI.Button(new Rect(rect.x + rect.width - 55, rect.y, 55, rect.height - 4), "Unload", EditorStyles.miniButton))
                {
                    GameObject.DestroyImmediate(sceneInstance.gameObject);
                }
                else if (!existsInScene && GUI.Button(new Rect(rect.x + rect.width - 55, rect.y, 55, rect.height - 4), "Load", EditorStyles.miniButton))
                {
                    AbstractView viewAsset = AssetDatabase.LoadAssetAtPath <AbstractView>(assetPath);
                    if (viewAsset != null)
                    {
                        AbstractView instance = PrefabUtility.InstantiatePrefab(viewAsset) as AbstractView;
                        instance.SetParent(_propertyViewParent.objectReferenceValue as Transform, ViewDisplayMode.Overlay);

                        Selection.activeGameObject = instance.gameObject;
                    }
                    else
                    {
                        Debug.LogErrorFormat("Unable to load {0} ({1}), missing an AbstractView component", viewName, assetPath);
                    }
                }
            }
            else
            {
                EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width, rect.height), string.Format("{0} (Asset Missing)", viewName), EditorStyles.boldLabel);
                requiresRebuild = true;
            }
        }
예제 #2
0
        protected virtual AbstractView CreateView(ViewAsset asset, ViewDisplayMode displayMode)
        {
            if (_debug)
            {
                Debug.LogFormat("[ViewController] Creating View: {0}, displayMode: {1}", asset.viewType.Name, displayMode);
            }

            // load the view resource
            GameObject resource = asset.Load() as GameObject;

            if (resource != null)
            {
                // create an instance of the view resource
                AbstractView view = (Instantiate(resource) as GameObject).GetComponent <AbstractView>();

                if (view == null)
                {
                    Unload(asset.viewType);
                    throw new UnityException(string.Format("Resource for {0} has no view component attached!", asset.viewType));
                }

                // setup view inside viewParent
                view.SetParent(viewParent, displayMode);

                // finish view creation
                view._Create(this, displayMode);

                if (EventViewCreated != null)
                {
                    EventViewCreated(this, asset.viewType, displayMode);
                }

                return(view);
            }
            else
            {
                throw new UnityException(string.Format("Resource not found for: {0}", asset.viewType));
            }
        }