Exemple #1
0
        public static void Strip()
        {
            if (!BuildPipeline.isBuildingPlayer)
            {
                return;
            }

            Debug.Log($"Stripping Editor-Only objects.");

            var deletedBehaviours  = new List <MonoBehaviour>();
            var deletedGameObjects = new List <GameObject>();

            foreach (var behaviour in Object.FindObjectsOfType <MonoBehaviour>())
            {
                var type = behaviour.GetType();

                if (ConditionalComponentHelpers.IsComponentEditorOnly(type))
                {
                    deletedBehaviours.Add(behaviour);
                }

                var behaviourGameObject = behaviour.gameObject;

                if (!deletedGameObjects.Contains(behaviourGameObject) &&
                    ConditionalComponentHelpers.IsGameObjectEditorOnly(type))
                {
                    deletedGameObjects.Add(behaviourGameObject);
                }
            }

            Debug.Log($"Stripping {deletedGameObjects.Count} game object(s) & {deletedBehaviours.Count} component(s).");

            foreach (var deletedBehaviour in deletedBehaviours)
            {
                try
                {
                    Object.DestroyImmediate(deletedBehaviour);
                }
                catch (Exception e)
                {
                    throw new BuildFailedException(e);
                }
            }

            foreach (var deletedGameObject in deletedGameObjects)
            {
                try
                {
                    Object.DestroyImmediate(deletedGameObject);
                }
                catch (Exception e)
                {
                    throw new BuildFailedException(e);
                }
            }
        }
        private static void OnInitialization()
        {
            Application.quitting += Clear;

            var managerTypes = AppDomain.CurrentDomain.GetAssemblies()
                               .SelectMany(assembly => assembly.GetTypes()
                                           .Where(t => !t.IsAbstract && typeof(Component).IsAssignableFrom(t)))
                               .Select(type => (type, type.GetCustomAttribute <HiraManagerAttribute>()))
                               .Where(tuple => tuple.Item2 != null);

#if !UNITY_EDITOR
            managerTypes = managerTypes.Where(t =>
                                              !ConditionalComponentHelpers.IsComponentEditorOnly(t.type) && !ConditionalComponentHelpers.IsGameObjectEditorOnly(t.type));
#endif

            managerTypes = managerTypes.OrderByDescending(tuple => tuple.Item2.Priority);

            foreach (var(t, m) in managerTypes)
            {
                // checking if an instance is already present
                var alreadyPresent = Object.FindObjectOfType(t) as Component;
                if (alreadyPresent != null && alreadyPresent.GetType() == t)
                {
                    Add(t, alreadyPresent);
                    continue;
                }

                // checking if a default prefab is provided
                if (m.DefaultPrefabLocation != null)
                {
                    var prefab = Resources.Load <GameObject>(m.DefaultPrefabLocation);
                    if (prefab == null)
                    {
                        prefab = Resources.Load <GameObject>($"Default_{m.DefaultPrefabLocation}");
                    }
                    if (prefab == null)
                    {
                        Debug.LogErrorFormat($"Target prefab at {m.DefaultPrefabLocation} could not be found.");
                    }
                    else
                    {
                        var instantiatedObject = Object.Instantiate(prefab);
                        instantiatedObject.name = instantiatedObject.name.Replace("(Clone)", "");
                        var component = instantiatedObject.GetComponent(t);
                        // checking if the instantiated object has the component
                        if (component == null)
                        {
                            Debug.LogFormat(LogType.Error, LogOption.NoStacktrace, null,
                                            $"Target prefab at {m.DefaultPrefabLocation} does not contain " +
                                            $"a {t.Name} component. Adding one to the instance.");
                            component = instantiatedObject.AddComponent(t);
                        }

                        Add(t, component);
                        continue;
                    }
                }

                var addedComponent = new GameObject($"[{t.Name}]").AddComponent(t);
                Add(t, addedComponent);
#if LOG_HIRA_MANAGERS
                Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null,
                                $"<color=green>Created HiraManager: </color>{addedComponent.gameObject.name}");
#endif
            }
        }