public override void DuplicateItem(int index)
        {
            var source = crudList[index];

            var item = UnityEngine.Object.Instantiate <InventoryItemBase>(source);

            item.ID    = (crudList.Count > 0) ? crudList.Max(o => o.ID) + 1 : 0;
            item.name += "(duplicate)";

            string prefabPath = InventoryScriptableObjectUtility.GetSaveFolderForFolderName("Items") + "/item_" + System.DateTime.Now.ToFileTimeUtc() + "_PFB.prefab";

            var prefab = PrefabUtility.CreatePrefab(prefabPath, item.gameObject);

            prefab.layer = InventorySettingsManager.instance.settings.itemWorldLayer;

            AssetDatabase.SetLabels(prefab, new string[] { "InventoryProPrefab" });

            AddItem(prefab.gameObject.GetComponent <InventoryItemBase>());

            EditorUtility.SetDirty(prefab);                              // To save it.

            UnityEngine.Object.DestroyImmediate(item.gameObject, false); // Destroy the instance created

            window.Repaint();
        }
Beispiel #2
0
        protected virtual void RenameScriptableObjectIfNeeded(T obj, string name)
        {
            if (obj == null)
            {
                return;
            }

            var nameWithExtension = name ?? "";

            if (nameWithExtension.EndsWith(".asset") == false)
            {
                nameWithExtension += ".asset";
            }

            var assetPath = AssetDatabase.GetAssetPath(obj);

            if (assetPath.EndsWith(nameWithExtension) == false)
            {
                var saveFolder = InventoryScriptableObjectUtility.GetSaveFolderForType(typeof(T));
                var renamePath = saveFolder + "/" + nameWithExtension;
                if (AssetDatabase.LoadAssetAtPath <T>(renamePath) == null)
                {
                    RenameScriptableObject(obj, name ?? "nameless");
                }
            }
        }
Beispiel #3
0
        protected override void CreateNewItem()
        {
            var item = ScriptableObjectUtility.CreateAsset <T>(InventoryScriptableObjectUtility.GetSaveFolderForType(typeof(T)), DateTime.Now.ToFileTimeUtc() + ".asset");

            GiveItemNewID(item);
            AddItem(item, true);
        }
Beispiel #4
0
        protected override void DrawDetail(InventoryItemBase item, int index)
        {
            EditorGUIUtility.labelWidth = EditorStyles.labelWidth;

            if (InventoryScriptableObjectUtility.isPrefabsSaveFolderSet == false)
            {
                EditorGUILayout.HelpBox("Prefab save folder is not set.", MessageType.Error);
                if (GUILayout.Button("Set prefab save folder"))
                {
                    InventoryScriptableObjectUtility.SetPrefabSaveFolder();
                }

                EditorGUIUtility.labelWidth = 0;
                return;
            }

            GUILayout.Label("Use the inspector if you want to add custom components.", EditorStyles.titleStyle);
            EditorGUILayout.Space();
            EditorGUILayout.Space();

            if (GUILayout.Button("Convert type"))
            {
                var typePicker = ScriptPickerEditor.Get(typeof(InventoryItemBase));
                typePicker.Show();
                typePicker.OnPickObject += type =>
                {
                    ConvertThisToNewType(item, type);
                };

                return;
            }

            //EditorGUI.BeginChangeCheck();
            var instance = (InventoryItemBase)itemEditorInspector.target;

            itemEditorInspector.OnInspectorGUI();

            if (_previouslySelectedGUIItemName == "ItemEditor_itemName" && GUI.GetNameOfFocusedControl() != _previouslySelectedGUIItemName)
            {
                //UpdateAssetName(item);
                if (itemEditorInspector.target is InventoryItemBase)
                {
                    // TODO Remove this
                }
            }

            //if (EditorGUI.EndChangeCheck() && selectedItem != null)
            if (GUI.changed || !EditorIsSaved())
            {
                selectedItem = ItemManager.database.items[index] = UpdatePrefab(instance, item);
                UnityEditor.EditorUtility.SetDirty(selectedItem);

                SetEditorIsSaved();
            }

            _previouslySelectedGUIItemName = GUI.GetNameOfFocusedControl();

            ValidateItemFromCache(item);
            EditorGUIUtility.labelWidth = 0;
        }
        protected override void CreateNewItem()
        {
            var picker = CreateNewItemEditor.Get((System.Type type, GameObject obj, EditorWindow thisWindow) =>
            {
                InventoryScriptableObjectUtility.SetPrefabSaveFolderIfNotSet();
                string prefabPath = InventoryScriptableObjectUtility.GetSaveFolderForFolderName("Items") + "/item_" + System.DateTime.Now.ToFileTimeUtc() + "_PFB.prefab";

                //var obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                var instanceObj = UnityEngine.Object.Instantiate <GameObject>(obj); // For unity 5.3+ - Source needs to be instance object.
                var prefab      = PrefabUtility.CreatePrefab(prefabPath, instanceObj);
                UnityEngine.Object.DestroyImmediate(instanceObj);

                if (InventorySettingsManager.instance != null && InventorySettingsManager.instance.settings != null)
                {
                    prefab.layer = InventorySettingsManager.instance.settings.itemWorldLayer;
                }
                else
                {
                    Debug.LogWarning("Couldn't set item layer because there's no InventorySettingsManager in the scene");
                }

                AssetDatabase.SetLabels(prefab, new string[] { "InventoryProPrefab" });

                var comp = (InventoryItemBase)prefab.AddComponent(type);
                comp.ID  = (crudList.Count > 0) ? crudList.Max(o => o.ID) + 1 : 0;
                EditorUtility.SetDirty(comp); // To save it.

                prefab.GetOrAddComponent <ItemTrigger>();
                prefab.GetOrAddComponent <ItemTriggerInputHandler>();
                if (prefab.GetComponent <SpriteRenderer>() == null)
                {
                    // This is not a 2D object
                    if (prefab.GetComponent <Collider>() == null)
                    {
                        prefab.AddComponent <BoxCollider>();
                    }

                    var sphereCollider       = prefab.GetOrAddComponent <SphereCollider>();
                    sphereCollider.isTrigger = true;
                    sphereCollider.radius    = 1f;

                    prefab.GetOrAddComponent <Rigidbody>();
                }

                // Avoid deleting the actual prefab / model, only the cube / internal models without an asset path.
                if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(obj)))
                {
                    Object.DestroyImmediate(obj);
                }

                AddItem(comp, true);
                thisWindow.Close();
            });

            picker.Show();
        }
        public override void Draw()
        {
            if (InventoryScriptableObjectUtility.isPrefabsSaveFolderSet == false || InventoryScriptableObjectUtility.isPrefabsSaveFolderValid == false)
            {
                GUI.color = Color.red;
            }

            EditorGUILayout.BeginHorizontal(EditorStyles.boxStyle);
            EditorGUILayout.LabelField(InventoryPro.ProductName + " prefab save folder: " + InventoryScriptableObjectUtility.prefabsSaveFolder);
            GUI.color = Color.white;

            if (GUILayout.Button("Set path", GUILayout.Width(100)))
            {
                InventoryScriptableObjectUtility.SetPrefabSaveFolder();
            }

            EditorGUILayout.EndHorizontal();

            base.Draw();
        }