//Try to drop the item to ground private void DropItem() { //Get the item to drop Item item = dragObject != null ? dragObject.item : ObservedItem; //Check if the item is droppable if (item != null && item.IsDroppable) { //Get item prefab GameObject prefab = item.OverridePrefab != null ? item.OverridePrefab : item.Prefab; RaycastHit hit; Vector3 position = Vector3.zero; Vector3 forward = Vector3.zero; if (InventoryManager.current.PlayerInfo.transform != null) { position = InventoryManager.current.PlayerInfo.transform.position; forward = InventoryManager.current.PlayerInfo.transform.forward; } //Cast a ray from mouse postion to ground if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit) && !UnityTools.IsPointerOverUI()) { //Clamp the drop distance to max drop distance defined in setting. Vector3 worldPos = hit.point; Vector3 diff = worldPos - position; float distance = diff.magnitude; if (distance > (InventoryManager.DefaultSettings.maxDropDistance - (transform.localScale.x / 2))) { position = position + (diff / distance) * InventoryManager.DefaultSettings.maxDropDistance; } else { position = worldPos; } } else { position = position + forward; } //Instantiate the prefab at position GameObject go = InventoryManager.Instantiate(prefab, position + Vector3.up * 0.3f, Quaternion.identity); go.name = go.name.Replace("(Clone)", ""); //Reset the item collection of the prefab with this item ItemCollection collection = go.GetComponent <ItemCollection>(); if (collection != null) { collection.Clear(); collection.Add(item); } ItemContainer.RemoveItemCompletely(item); Container.NotifyDropItem(item, go); } }
private IEnumerator Refill(ItemCollection collection) { while (true) { if (collection.IsEmpty) { yield return(new WaitForSeconds(m_Refill)); collection.Add(GenerateItems().ToArray()); } yield return(new WaitForSeconds(3f)); } }
private void DropItem(Item item) { GameObject prefab = item.OverridePrefab != null ? item.OverridePrefab : item.Prefab; float angle = Random.Range(0f, 360f); float x = (float)(InventoryManager.DefaultSettings.maxDropDistance * Mathf.Cos(angle * Mathf.PI / 180f)) + gameObject.transform.position.x; float z = (float)(InventoryManager.DefaultSettings.maxDropDistance * Mathf.Sin(angle * Mathf.PI / 180f)) + gameObject.transform.position.z; Vector3 position = new Vector3(x, 1f, z); GameObject go = InventoryManager.Instantiate(prefab, position, Random.rotation); ItemCollection collection = go.GetComponent <ItemCollection>(); if (collection != null) { collection.Clear(); collection.Add(item); } }
private void SetupPrefab(SerializedProperty prefabProperty) { if (prefabProperty.objectReferenceValue != null) { GameObject mPrefab = prefabProperty.objectReferenceValue as GameObject; if (mPrefab.GetComponent <Trigger>() == null || mPrefab.GetComponent <Collider>() == null || mPrefab.GetComponent <Rigidbody>() == null) { Color color = GUI.backgroundColor; GUI.backgroundColor = Color.red; if (GUILayout.Button("Setup", GUILayout.Width(70))) { GameObject prefab = (GameObject)Instantiate(mPrefab); if (prefab.GetComponent <Trigger>() == null) { Trigger trigger = prefab.AddComponent <Trigger>(); //trigger.actions.Add(prefab.AddComponent<CanPickup>()); /*SetEnabled setEnabled = prefab.AddComponent<SetEnabled>(); * SerializedObject setEnabledObject = new SerializedObject(setEnabled); * setEnabledObject.Update(); * setEnabledObject.FindProperty("m_ComponentName").stringValue = "ThirdPersonController"; * setEnabledObject.FindProperty("m_Enable").boolValue = false; * setEnabledObject.ApplyModifiedProperties(); * trigger.actions.Add(setEnabled); * trigger.actions.Add(prefab.AddComponent<LookAtTrigger>()); * trigger.actions.Add(prefab.AddComponent<CrossFade>()); * trigger.actions.Add(prefab.AddComponent<Wait>()); * trigger.actions.Add(prefab.AddComponent<Pickup>()); * setEnabled = prefab.AddComponent<SetEnabled>(); * setEnabledObject = new SerializedObject(setEnabled); * setEnabledObject.Update(); * setEnabledObject.FindProperty("m_ComponentName").stringValue = "ThirdPersonController"; * setEnabledObject.FindProperty("m_Enable").boolValue = true; * setEnabledObject.ApplyModifiedProperties(); * trigger.actions.Add(setEnabled);*/ } if (prefab.GetComponent <ItemCollection>() == null) { ItemCollection collection = prefab.AddComponent <ItemCollection>(); collection.Add((Item)target); } if (prefab.GetComponent <Collider>() == null) { MeshCollider collider = prefab.AddComponent <MeshCollider>(); collider.convex = true; } if (prefab.GetComponent <Rigidbody>() == null) { prefab.AddComponent <Rigidbody>(); } #if PUN if (prefab.GetComponent <PhotonView> () == null) { prefab.AddComponent <PhotonView> (); } #endif string mPath = EditorUtility.SaveFilePanelInProject( "Create Prefab" + prefab.name, "New " + prefab.name + ".prefab", "prefab", ""); if (!string.IsNullOrEmpty(mPath)) { GameObject mGameObject = PrefabUtility.SaveAsPrefabAsset(prefab, mPath); AssetDatabase.SaveAssets(); prefabProperty.objectReferenceValue = mGameObject; } DestroyImmediate(prefab); } GUI.backgroundColor = color; } } }
private void Start() { ItemCollection collection = GetComponent <ItemCollection>(); collection.Add(GenerateItems().ToArray()); }