Пример #1
0
        /// <summary>
        /// Spawns object using the middle of the screen as position of the raycast
        /// </summary>
        /// <param name="obj"></param>
        public void SpawnObjectMiddle(GameObject obj)
        {
            Vector3    location;
            Quaternion rotation = new Quaternion(0, 0, 0, 0);

            Vector2 mousePos = new Vector2 {
                x = Screen.width / 2,
                y = Screen.height / 2
            };

            Ray ray = Cam.ScreenPointToRay(mousePos);

            GameObject spawnedObj;

            //Debug.Log(obj.name);
            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                var Dist = Vector3.Distance(hit.transform.position, Cam.transform.position);
                Dist      /= 2;
                location   = Cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Dist));
                spawnedObj = Instantiate(obj, location, rotation, ParentObject.transform);
            }
            else
            {
                location   = Cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 500));
                spawnedObj = Instantiate(obj, location, rotation, ParentObject.transform);
            }

            RemoveHighlights(_selectedObjects);
            _selectedObjects.Clear();

            // for .obj files that have child objects with meshes
            List <GameObject> children = spawnedObj.GetAllChildren();

            if (children.Count > 0)               // if the gameobject has no children it doesn't need to loop over anything
            {
                foreach (GameObject child in children)
                {
                    // For some reason only Destroy makes it work. when using DeleteObject() it deletes the wrong child object with the same name.
                    if (child.GetComponent <MeshRenderer>() == null)
                    {
                        //Debug.Log("annihilate : " + child.name);
                        Destroy(child);
                    }
                    else
                    {
                        // This is making sure the all the objects spawned have the same name as the parent so that the can be selected
                        // The names dont have to be different because the addObjectToObjectListMenu() function takes care of duplicate names.
                        child.name = spawnedObj.name;
                        // UnParent the childobjects and set them under the Objects Parent object
                        child.transform.SetParent(ParentObject.transform);

                        // Add children to the sceneObjectsList
                        _selectedObjects.Add(child);
                        scene.AddObjectToObjectListMenu(child);

                        AddOrigin(child, obj.name, true);
                    }
                }
            }

            if (spawnedObj.GetComponent <Collider>() == null && spawnedObj.GetComponent <MeshCollider>() == null)
            {
                //Debug.Log("Annihilate : " + spawnedObj.name);
                Destroy(spawnedObj);
            }
            else
            {
                AddOrigin(spawnedObj, obj.name, false);
                _selectedObjects.Add(spawnedObj);
                scene.AddObjectToObjectListMenu(spawnedObj);
            }
            OnSelectionChanged();
        }
    /// <summary>
    /// Function for obtaining GameObject from ObjectData
    /// </summary>
    /// <param name="objData">Deserialized ObjectData</param>
    /// <returns>UnityEngine.GameObject</returns>
    public GameObject GetGameObjectFromObjectData(ObjectData objData)
    {
        // usefull for possible oneliner\______________________________________________________________________________/ could fore go the entire foreach loop
        // GameObject obj = Instantiate(assets.FirstOrDefault(asset => asset.name == objData.originData.originObjectName), parentObject.transform) as GameObject;

        foreach (var asset in assets)
        {
            if (asset.name == objData.originData.originObjectName)
            {
                // Checks if the name of the origin object is an asset.
                // There should always be an asset. Only if its a separate parent object it won't.

                GameObject obj = Instantiate(asset, parentObject.transform) as GameObject;

                if (objData.originData.originObjectHasMultipleMeshes)
                {
                    List <GameObject> children = GetAllChildren(obj);
                    foreach (GameObject child in children)
                    {
                        // For some reason only Destroy makes it work. when using DeleteObject() it deletes the wrong child object with the same name.
                        if (child.name == objData.originData.meshName)
                        {
                            // Gives the Child the correct name
                            child.name = objData.objectName;
                            // UnParent the childobject and set them under the Objects parentObject
                            Destroy(obj);
                            child.transform.SetParent(parentObject.transform);
                            // Add Child to the sceneObjectsList
                            obj = child;
                            break;
                        }
                        else
                        {
                            //Debug.Log("annihilate : " + child.name);
                            Destroy(child);
                        }
                    }
                }

                //Debug.Log("Instantiate object");

                if (obj.GetComponent <Renderer>() != null)                  // sets color if an asset and render component is found
                {
                    obj.GetComponent <Renderer>().material.color = objData.objectColor.GetColor();
                    // Other data related to Rendering the object has to be placed here
                    // Stuff Like:
                    // - Background Image
                    // - Smoothness
                    // - Metallic
                }

                obj.name = objData.objectName;
                obj.transform.localPosition = objData.position;
                obj.transform.localRotation = Vector4ToQuaternion(objData.rotation);
                obj.transform.localScale    = objData.scale;

                OriginObject origin;
                if (obj.GetComponent <OriginObject>() == null)
                {
                    origin = obj.AddComponent <OriginObject>();
                }
                else
                {
                    origin = obj.GetComponent <OriginObject>();
                }

                origin.originObjectName = objData.originData.originObjectName;
                origin.originObjectHasMultipleMeshes = objData.originData.originObjectHasMultipleMeshes;
                origin.meshName = objData.originData.meshName;

                // And lastly add the object to the scene list
                SceneManager.AddObjectToObjectListMenu(obj);
                return(obj);
            }
        }
        // If for some reason there is no asset for the origin object
        // Debug.Log(obj.name);
        Debug.Log("No Asset Found for:" + objData.originData.originObjectName);
        return(new GameObject("No Asset Found for:" + objData.objectName));
    }