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