Exemplo n.º 1
0
        void CreateUnitPrefab(UnitData unitData)
        {
            var unitGameObject = new GameObject("Unit" + unitData.textId);

            unitGameObject.layer = LayerMask.NameToLayer("Unit");

            var unitModule = unitGameObject.AddComponent <Unit>();

            unitModule.SetUnitData(unitData);

            var model = new GameObject("Model");

            model.transform.SetParent(unitGameObject.transform);
            model.transform.localPosition = Vector3.zero;

            var realModel = Instantiate(unitData.unitModel, model.transform);

            realModel.transform.localPosition = Vector3.zero;

            if (unitData.addsElectricity > 0 || unitData.usesElectricity > 0)
            {
                unitGameObject.AddComponent <ElectricityModule>();
            }

            if (!unitGameObject.GetComponent <BoxCollider>())
            {
                unitGameObject.AddComponent <BoxCollider>();
            }

            if (unitData.canBeDestroyed)
            {
                unitGameObject.AddComponent <Damageable>();
            }

            if (unitData.hasMoveModule)
            {
                unitGameObject.AddComponent <Movable>();
            }

            if (unitData.hasAttackModule)
            {
                var attackable  = unitGameObject.AddComponent <Attackable>();
                var shootPoints = model.transform.GetAllChilds(true).FindAll(ch => ch.name.ToLower().StartsWith("shootpoint"));

                attackable.SetShootPoints(shootPoints);
            }

            if (unitData.hasTurret)
            {
                var turret          = unitGameObject.AddComponent <Tower>();
                var turretTransform = model.transform.GetAllChilds(true).Find(ch => ch.name.ToLower().StartsWith("turretbone"));

                turret.SetTurretTransform(turretTransform);
            }

            if (unitData.isHarvester)
            {
                unitGameObject.AddComponent <Harvester>();
            }

            if (unitData.isRefinery)
            {
                unitGameObject.AddComponent <Refinery>();
            }

            if (unitData.isProduction)
            {
                for (var i = 0; i < unitData.productionCategories.Count; i++)
                {
                    var production = unitGameObject.AddComponent <Production>();
                    production.SetCategoryId(i);

                    // todo find spawnpoint and waypoint bones and set it to prod
                }
            }

            if (unitData.isInfantry)
            {
                unitGameObject.AddComponent <Infantry>();
                var animator = unitGameObject.AddComponent <Animator>();

                if (unitData.animatorController)
                {
                    animator.runtimeAnimatorController = unitData.animatorController;
                }
            }

            unitGameObject.AddComponent <FogOfWarModule>();

            if (unitData.canCarryUnitsCount > 0)
            {
                unitGameObject.AddComponent <CarryModule>();
            }

            var wheelsBones = model.transform.GetAllChilds(true).FindAll(ch => ch.name.ToLower().StartsWith("wheelbone"));

            if (wheelsBones.Count > 0)
            {
                var wheelsComponent = unitGameObject.AddComponent <Wheels>();
                wheelsComponent.SetupWheels(wheelsBones);
            }

            unitGameObject.AddComponent <AudioSource>();

            if (!AssetDatabase.IsValidFolder("Assets/Prefabs"))
            {
                AssetDatabase.CreateFolder("Assets", "Prefabs");
            }

            if (!AssetDatabase.IsValidFolder("Assets/Prefabs/Units"))
            {
                AssetDatabase.CreateFolder("Assets/Prefabs", "Units");
            }

            // todo add support of 2019 etc - SaveAsAsset or what

            var prefab = PrefabUtility.CreatePrefab("Assets/Prefabs/Units/Unit" + unitData.textId + ".prefab", unitGameObject);

            PrefabUtility.ConnectGameObjectToPrefab(unitGameObject, prefab);
            //AssetDatabase.Refresh();

            unitData.selfPrefab = prefab;             // PrefabUtility.GetCorrespondingObjectFromSource(unitGameObject) as GameObject;
            EditorUtility.SetDirty(unitData);
        }