Пример #1
0
        private void CreateCode()
        {
            if (_modelsList.All(x => x.Skip))
            {
                CancelCreating();
                Debug.Log("No objects to create code");
                return;
            }

            foreach (CreateObjectModel fileModel in _modelsList)
            {
                if (fileModel.Skip)
                {
                    continue;
                }

                _currentStep = "Creating code for " + fileModel.ObjectName;
                Debug.Log(_currentStep);

                fileModel.ClassName = CreateObjectWindow.CreateCode(fileModel);
                Debug.Log("Class " + fileModel.ClassName + " created.");
            }

            CreateObjectTempModel temp = new CreateObjectTempModel()
            {
                Objects = _modelsList, BuildNow = _buildNow
            };

            string jsonModels = JsonConvert.SerializeObject(temp, Formatting.None,
                                                            new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            File.WriteAllText(CreateObjectTempModel.TempFilePath, jsonModels);


            _currentStep = "Compiling scripts...";
            AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
        }
Пример #2
0
        private static void OnScriptsReloaded()
        {
            try
            {
                GetWindow <ImportModelsWindow>().Close();

                int createdNum = 0;

                if (File.Exists(CreateObjectTempModel.TempFilePath))
                {
                    string config = File.ReadAllText(CreateObjectTempModel.TempFilePath);

                    CreateObjectTempModel temp = JsonConvert.DeserializeObject <CreateObjectTempModel>(config);

                    File.Delete(CreateObjectTempModel.TempFilePath);

                    if (temp == null)
                    {
                        Debug.LogError("Temp build file is broken! Can't finish objects creation.");

                        return;
                    }

                    _modelsList = temp.Objects;
                    _buildNow   = temp.BuildNow;

                    foreach (CreateObjectModel fileModel in _modelsList)
                    {
                        Debug.Log("Creating prefab for " + fileModel.ObjectName);

                        GameObject gameObject, prefab;

                        //Check if it's model import
                        if (fileModel.ModelImportPath != null)
                        {
                            if (fileModel.Skip)
                            {
                                continue;
                            }

                            GameObject modelPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(fileModel.ModelImportPath);

                            if (modelPrefab == null)
                            {
                                Debug.LogError("Can't create object " +
                                               fileModel.ObjectName +
                                               ". Imported file is incorrect: " +
                                               fileModel.Path);
                                Directory.Delete(fileModel.ObjectFolder, true);

                                continue;
                            }

                            gameObject = Instantiate(modelPrefab, new Vector3(0, 0, 0), Quaternion.identity);

                            //Calculate bounds
                            Bounds bounds   = GetBounds(gameObject);
                            float  maxBound = Mathf.Max(bounds.size.x, bounds.size.y, bounds.size.z);
                            float  scale    = fileModel.BiggestSideSize / maxBound;

                            gameObject.transform.localScale = Vector3.one * scale;
                            Rigidbody objectBody = gameObject.AddComponent <Rigidbody>();
                            objectBody.isKinematic = !fileModel.IsPhysicsOn;
                            objectBody.mass        = fileModel.Mass;

                            InteractableObjectBehaviour objectBehaviour =
                                gameObject.AddComponent <InteractableObjectBehaviour>();
                            objectBehaviour.SetIsGrabbable(fileModel.IsGrabbable);
                            objectBehaviour.SetIsUsable(false);
                            objectBehaviour.SetIsTouchable(false);

                            CreateObjectUtils.AddObjectId(gameObject);

                            MeshFilter[] meshes = gameObject.GetComponentsInChildren <MeshFilter>();

                            foreach (MeshFilter meshFilter in meshes)
                            {
                                MeshCollider collider = meshFilter.gameObject.AddComponent <MeshCollider>();
                                collider.sharedMesh = meshFilter.sharedMesh;
                                collider.convex     = true;
                            }

                            if (meshes == null || meshes.Length == 0)
                            {
                                BoxCollider box = gameObject.AddComponent <BoxCollider>();

                                box.center = bounds.center;
                                box.size   = bounds.size;
                            }

                            prefab = CreatePrefab(gameObject,
                                                  fileModel.PrefabPath,
                                                  fileModel.Guid,
                                                  fileModel.ObjectName,
                                                  fileModel.ClassName,
                                                  fileModel.Extras);
                        }
                        else
                        {
                            gameObject = AssetDatabase.LoadAssetAtPath <GameObject>(fileModel.PrefabPath);

                            if (gameObject == null)
                            {
                                Debug.LogError("Can't create " +
                                               fileModel.ObjectName +
                                               ": no prefab and no model. How did it happen? Please try again.");

                                return;
                            }

                            CreateNew(gameObject,
                                      fileModel.PrefabPath,
                                      fileModel.Guid,
                                      fileModel.ObjectName,
                                      fileModel.ClassName,
                                      fileModel.Extras,
                                      true,
                                      true);
                        }

                        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

                        if (fileModel.ModelImportPath != null)
                        {
                            DestroyImmediate(gameObject);
                        }

                        createdNum++;

                        gameObject = AssetDatabase.LoadAssetAtPath <GameObject>(fileModel.PrefabPath);

                        var instance = PrefabUtility.InstantiatePrefab(gameObject);
                        instance.name = instance.name.Replace("(Clone)", "");
                        EditorGUIUtility.PingObject(instance);
                    }

                    EditorUtility.DisplayDialog("Done!", createdNum + " objects were created!", "OK");

                    if (temp.BuildNow)
                    {
                        ObjectsBuilderWindow.BuildObjects(_modelsList.ToArray());
                    }


                    GetWindow <CreateObjectWindow>().Close();
                }
            }
            catch (Exception e)
            {
                EditorUtility.DisplayDialog("Error!", $"{e.Message}:\nProblem when creating an objects", "OK");
                Debug.LogException(e);
            }
        }
Пример #3
0
        private void Create()
        {
            if (_gameObject == null)
            {
                EditorUtility.DisplayDialog("Error!", "GameObject can't be null.", "OK");

                return;
            }

            if (!ObjectBuildHelper.IsValidTypeName(_objectClassName, true))
            {
                return;
            }

            _localizedName   = ObjectBuildHelper.EscapeString(_localizedName);
            _localizedNameRU = ObjectBuildHelper.EscapeString(_localizedNameRU);

            CreateObjectModel model = new CreateObjectModel()
            {
                Guid            = Guid.NewGuid().ToString(),
                ObjectName      = _objectClassName.Replace(" ", ""),
                LocalizedName   = _localizedName,
                LocalizedNameRU = _localizedNameRU,
                MobileReady     = _mobileReady
            };

            model.ObjectFolder = "Assets/Objects/" + model.ObjectName;
            model.PrefabPath   = model.ObjectFolder + "/" + model.ObjectName + ".prefab";

            if (Directory.Exists(model.ObjectFolder))
            {
                EditorUtility.DisplayDialog(model.ObjectName + " already exists!",
                                            "Object with this name already exists!", "OK");
            }
            else
            {
                Debug.Log("Create folder " + model.ObjectFolder);
                Directory.CreateDirectory(model.ObjectFolder);

                Debug.Log("Create prefab " + _gameObject.name);

                CreatePrefab(_gameObject, model.PrefabPath, model.Guid,
                             _objectClassName, null, null,
                             false);
                CreateTags(model);

                model.ClassName = CreateCode(model);

                _modelsList = new List <CreateObjectModel>();
                _modelsList.Add(model);

                CreateObjectTempModel temp = new CreateObjectTempModel()
                {
                    Objects = _modelsList, BuildNow = false
                };

                string jsonModels = JsonConvert.SerializeObject(temp, Formatting.None,
                                                                new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                File.WriteAllText(CreateObjectTempModel.TempFilePath, jsonModels);
                AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
            }
        }