Пример #1
0
    public void AsyncLoadingScript(GameObject myReturnValue)
    {
        #region 读取模型
        transform.position += StartPointOffSet;
        InstanceMesh        = Instantiate(myReturnValue) as GameObject;
        InstanceMesh.transform.SetParent(this.transform);
        InstanceMesh.transform.localPosition    = Vector3.zero;
        InstanceMesh.transform.localEulerAngles = Vector3.zero;
        #endregion

                #if UNITY_EDITOR && UNITY_ANDROID
        foreach (MeshRenderer meshRenderer in InstanceMesh.GetComponentsInChildren <MeshRenderer>())
        {
            meshRenderer.sharedMaterial.shader = Shader.Find("Standard");
        }
                #endif

        #region 设置网络状态
        if (_InstanceNetType != InstanceNetType.GarageTank && !isBot(_InstanceNetType) && _InstanceNetType != InstanceNetType.GameNetWorkOffline)
        {
        }
        #endregion


        //模块加载
        dontDestroyManager = new VehicleDontDestroyManagerModule();

        referenceManager = InstanceMesh.GetComponent <VehicleComponentsReferenceManager>();


        #region 加载完毕游戏资源加载脚本
        TankPrefabs     = gameObject;
        TurretTransform = InstanceMesh.transform.Find("TurretTransform").gameObject;
        MainHitBox      = InstanceMesh.transform.Find("MainHitBox").gameObject;
        TankTransform   = InstanceMesh.transform.Find("TankTransform").gameObject;
        GunTransform    = TurretTransform.transform.Find("GunTransform").gameObject;
        FFPoint         = GunTransform.transform.Find("FFPoint").gameObject;
        EffectStart     = GunTransform.transform.Find("EffectStart").gameObject;
        TurretHitBox    = TurretTransform.transform.Find("TurretHitBox").gameObject;



        GameObject       TankScript = null, TankCrossHair = null, MainCamera = null;
        Rigidbody        TankPhysic = null;
        TurretController mt         = null;
//		SyncGroundVehicle _syncGroundVehicle = null;
        PlayerTracksController PTC = null;
        PlayerState            PS  = null;
        PTC = InstanceMesh.AddComponent <PlayerTracksController> ();

        #region 物理效果
        TankPhysic             = GetComponentInChildren <Rigidbody> ();
        TankPhysic.mass        = PTCParameter.Mass;
        TankPhysic.drag        = 0.1f; //PTCParameter.Drag;
        TankPhysic.angularDrag = 2.5f; //PTCParameter.AirDrag;
        TankPhysic.useGravity  = false;
        BoxCollider[] collisions = PTC.GetComponentsInChildren <BoxCollider> ();
        foreach (Collider collision in collisions)
        {
            if (collision.isTrigger != true)
            {
                collision.material = StaticResourcesReferences.Instance.VehiclePhysicMaterial;
            }
        }

        #endregion
        if (_InstanceNetType != InstanceNetType.GarageTank)
        {
            if (isLocalPlayer(_InstanceNetType))
            {
                TankCrossHair = Instantiate((GameObject)Resources.Load("TankCrossHair"), Vector3.zero, Quaternion.identity) as GameObject;
                MainCamera    = Instantiate((GameObject)Resources.Load("MainCamera"), Vector3.zero, Quaternion.identity) as GameObject;
                TankCrossHair.transform.parent = TankPrefabs.transform;
                MainCamera.transform.parent    = InstanceMesh.transform;
                MainCamera.transform.name      = "MainCamera";
            }
            TankScript = Instantiate((GameObject)Resources.Load("TankScript"), Vector3.zero, Quaternion.identity) as GameObject;

            TankScript.transform.name   = "TankScript";
            TankScript.transform.parent = TankPrefabs.transform;
            TankPhysic.useGravity       = true;

            #region PlayerState 脚本设置
            if (!isBot(_InstanceNetType))
            {
                PS = InstanceMesh.AddComponent <PlayerState>();
                PS.tankInitSystem = this;

                foreach (HitBox HB in GetComponentsInChildren <HitBox>())
                {
                    HB.SetTarget(PS);
                }
                PS.playerStateParameter = PSParameter;

                PS.netType            = _InstanceNetType;
                PS.Turret             = TurretTransform;
                PS.TankScript         = TankScript;
                PS.DiedDestoryObjects = new Object[] { MainHitBox, TurretHitBox };
                PS.TankName           = VehicleName;
                PS.Health             = PSParameter.Health;
                PS.engineType         = PSParameter.engineType;

                PS.IsMobile = isMobile;
            }
            #endregion
            if (isBot(_InstanceNetType))
            {
                EnemyAiState enemyState = InstanceMesh.AddComponent <EnemyAiState>();
                foreach (HitBox HB in GetComponentsInChildren <HitBox>())
                {
                    HB.SetTarget(enemyState);
                }
                enemyState.TankScript = TankScript;
                enemyState.Health     = PSParameter.Health;
                enemyState.TankName   = VehicleName;
                enemyState.MyTeam     = ownerTeam;
            }

            #region TankScript 脚本设置
            mt = TankScript.GetComponent <TurretController>();
            if (isLocalPlayer(_InstanceNetType))
            {
                mt.target = MainCamera.transform.Find("CameraTarget");
            }
            mt.gun                    = GunTransform.transform;
            mt.Turret                 = TurretTransform.transform;
            mt.DownMaxDegree          = MTParameter.DownMaxDegree;
            mt.UpMaxDegree            = MTParameter.UpMaxDegree;
            mt.maxTurretAngle         = MTParameter.maxTurretAngle;
            mt.gunDegreesPerSecond    = MTParameter.gunDegreesPerSecond;
            mt.turretDegreesPerSecond = MTParameter.turretDegreesPerSecond;
            mt.isMobile               = isMobile;
            #endregion
            #region 主摄像机设置
            if (isLocalPlayer(_InstanceNetType))
            {
                MainCamera.GetComponent <VehicleCamera>().target = referenceManager.MainCameraFollowTarget.transform;
                MainCamera.GetComponent <VehicleCamera>().mainCameraFollowTarget = referenceManager.MainCameraFollowTarget.transform;
                MainCamera.GetComponent <VehicleCamera>().mainCameraGunner       = referenceManager.MachineGunFFPoint.transform;
                referenceManager.MachineGunFFPoint.transform.position            = referenceManager.FFPoint.transform.position;
                MainCamera.GetComponent <VehicleCamera>().IsMobile = isMobile;
                if (PSParameter.vehicleType == VehicleType.SPG)
                {
                    MainCamera.GetComponent <VehicleCamera>().isVehicleSPG = true;
                }
            }

            #endregion
            #region 瞄准射线
            if (isLocalPlayer(_InstanceNetType))
            {
                RayManager rayManager = TankScript.AddComponent <RayManager>();
                rayManager.Init(FFPoint.transform);
            }
            #endregion
            #region 瞄准镜设置
            if (isLocalPlayer(_InstanceNetType))
            {
                TankCrossHair.GetComponent <CrossHair>().MainCamera = MainCamera.GetComponent <Camera>();
                TankCrossHair.GetComponent <CrossHair>().tankCamera = MainCamera.GetComponent <VehicleCamera>();
                TankCrossHair.GetComponent <CrossHair>().FFPoint    = EffectStart.transform;
            }
            #endregion
            #region 坦克发射器脚本设置
            TankFire tf = TankScript.GetComponent <TankFire>();
            tf.tankFireParameter = TFParameter;
            tf.tankInitSystem    = this;
            tf.MachineGunFFPoint = referenceManager.MachineGunFFPoint.transform;
            tf.FFPoint           = referenceManager.FFPoint.transform;
            tf.FireRecoilPoint   = referenceManager.FireForceFeedbackPoint.transform;
            tf.FireEffectPoint   = referenceManager.EffectStart.transform;
            tf.BulletCountList   = BulletCountList;
            tf.GunDym            = GunTransform.transform.Find("GunDym").GetComponent <Animator>();
            tf.MainBody          = InstanceMesh.transform;
            tf.netType           = _InstanceNetType;


            if (PSParameter.vehicleType == VehicleType.SPG)
            {
                tf.isAutoCaclulateGravity = true;
            }
            #endregion

            #region 引擎音效控制
            EngineSoundModule engineSoundModule = InstanceMesh.AddComponent <EngineSoundModule>();
            engineSoundModule.tankInitSystem = this;
            engineSoundModule.engineData     = PTCParameter.vehicleEngineSoundData;
            engineSoundModule.Init(referenceManager.EngineSound.transform);
            #endregion
            GameObject EngineSmoke = Instantiate <GameObject>(Resources.Load <GameObject>("EngineSmoke"));
            EngineSmoke.transform.SetParent(referenceManager.EngineSmoke.transform);
            EngineSmoke.transform.localPosition    = Vector3.zero;
            EngineSmoke.transform.localEulerAngles = Vector3.zero;

            //GameObject grounddust = Instantiate(Resources.Load<GameObject>("TrackEffect"));
            //grounddust.transform.SetParent(InstanceMesh..transform);
            //grounddust.transform.localPosition = Vector3.zero;
            //grounddust.transform.localEulerAngles = Vector3.zero;

            VehicleState vehicleState = InstanceMesh.AddComponent <VehicleState>();
            vehicleState.engineSoundModule    = engineSoundModule;
            vehicleState.tankTracksController = PTC;
        }

        #region 坦克控制器设置
        PTC.tankInitSystem = this;

        PTC.rightTrackUpperWheels = GetAllChilds(TankTransform.transform.Find("RightUpperWheel"));
        PTC.leftTrackUpperWheels  = GetAllChilds(TankTransform.transform.Find("LeftUpperWheel"));
        PTC.rightTrackWheels      = GetAllChilds(TankTransform.transform.Find("RightWheel"));
        PTC.leftTrackWheels       = GetAllChilds(TankTransform.transform.Find("LeftWheel"));
        PTC.leftTrack             = referenceManager.LeftTrack;
        PTC.rightTrack            = referenceManager.RightTrack;

        //PTC.leftTrackBones = CreateTrack (true, TankTransform.transform, ref PTC);
        //PTC.rightTrackBones = CreateTrack (false, TankTransform.transform, ref PTC);
        PTC.tankTransform = TankTransform.transform;



        PTC.maxAngularVelocity = PTCParameter.MaxAngularVelocity;
        PTC.MaxSpeed           = PTCParameter.MaxSpeed;
        PTC.MinSpeed           = PTCParameter.MinSpeed;
        PTC.COM       = PTCParameter.CenterOfGravity;
        PTC.PushSpeed = PTCParameter.PushSpeed;
        PTC.BackSpeed = PTCParameter.BackSpeed;

        PTC.sidewaysFrictionAsymptoteFactor = PTCParameter.SideWaysFrictionAsymptoteFactor;
        PTC.sidewaysFrictionExtremumFactor  = PTCParameter.SideWaysFrictionExtremumFactor;
        PTC.wheelCollider = PTCParameter.TankWheelCollider;

        PTC.wheelsAndBonesAxisSettings = new PlayerTracksController.WheelsAxisSettings();
        PTC.wheelsAndBonesAxisSettings.bonesPositionAxis  = TankTracksController.Axis.Z;
        PTC.wheelsAndBonesAxisSettings.wheelsPositionAxis = TankTracksController.Axis.Z;
        PTC.wheelsAndBonesAxisSettings.wheelsRotationAxis = TankTracksController.Axis.X;

        PTC.accelerationConfiguration           = PTCParameter.VAconfigSetting;
        PTC.rotationOnAccelerationConfiguration = PTCParameter.HAconfigSetting;
        PTC.rotationOnStayConfiguration         = PTCParameter.HAconfigSetting;


        #endregion

        #region 车库状态
        if (_InstanceNetType == InstanceNetType.GarageTank)
        {
            PTC.enabled            = false;
            TankPhysic.isKinematic = true;

            if (ShowHitBoxInspecter)
            {
                foreach (HitBox HB in GetComponentsInChildren <HitBox>())
                {
                    HB.StartCoroutine(HB.ShowArmorInfo());
                }
            }

            foreach (MeshCollider MC in transform.root.GetComponentsInChildren <MeshCollider>())
            {
                if (MC.GetComponent <HitBox> () == null)
                {
                    MC.enabled = false;
                }
            }
            //EventManager.StartListening ("TankInitSystem.InitTankShader", GarageInitTankShaderPreView);
            return;
        }
        #endregion

        if (ExtraTF && !isBot(_InstanceNetType))
        {
            for (int i = 0; i < multiTurrets.Length; i++)
            {
                GameObject TankScriptExtra = Instantiate((GameObject)Resources.Load("TankScript"), Vector3.zero, Quaternion.identity) as GameObject;
                TankScriptExtra.transform.name   = "TankScriptExtra";
                TankScriptExtra.transform.parent = TankPrefabs.transform;
                TurretController mouseTurretExtra = TankScriptExtra.GetComponent <TurretController> ();
                if (isLocalPlayer(_InstanceNetType))
                {
                    mouseTurretExtra.target = MainCamera.transform.Find("CameraTarget");
                }
                Transform ExtraTurret = InstanceMesh.transform.Find(multiTurrets [i].ObjectPath);

                Transform ExtraGun = ExtraTurret.transform.Find("GunTransform");

                mouseTurretExtra.gun                    = ExtraGun;
                mouseTurretExtra.Turret                 = ExtraTurret;
                mouseTurretExtra.DownMaxDegree          = multiTurrets [i].MTParameter.DownMaxDegree;
                mouseTurretExtra.UpMaxDegree            = multiTurrets [i].MTParameter.UpMaxDegree;
                mouseTurretExtra.maxTurretAngle         = multiTurrets [i].MTParameter.maxTurretAngle;
                mouseTurretExtra.gunDegreesPerSecond    = multiTurrets [i].MTParameter.gunDegreesPerSecond;
                mouseTurretExtra.turretDegreesPerSecond = multiTurrets [i].MTParameter.turretDegreesPerSecond;
                mouseTurretExtra.isMobile               = isMobile;

                TankFire  tfExtra         = TankScriptExtra.GetComponent <TankFire> ();
                Transform ExtraFFPoint    = ExtraGun.Find("FFPoint");
                Transform ExtraFireEffect = ExtraGun.Find("FireEffect");

                tfExtra.FFPoint         = ExtraFFPoint;
                tfExtra.FireRecoilPoint = FFPoint.transform;
                tfExtra.FireEffectPoint = ExtraFireEffect;
                tfExtra.MainBody        = InstanceMesh.transform;
                //tfExtra.FireRecoil = multiTurrets [i].tankFireParameter.FireRecoil;
                //tfExtra.ReloadTime = multiTurrets [i].tankFireParameter.ReloadTime;
                //tfExtra.AmmoCount = multiTurrets [i].tankFireParameter.AmmoCount;
                //tfExtra.HasMahineGun = multiTurrets [i].tankFireParameter.HasMachineGun;
                //tfExtra.muzzleFire = multiTurrets [i].tankFireParameter.MuzzleFire;
                //tfExtra.advanceFireClass = multiTurrets [i].tankFireParameter.advanceFireClass;
                //tfExtra.fireState = multiTurrets [i].tankFireParameter.FireState;
                //tfExtra.netType = InstanceNetType;
            }
        }
        #endregion

        InstanceMesh.AddComponent <Identity> ();
        if (isLocalPlayer(_InstanceNetType))
        {
            GameEvent.InitPlayer(InstanceMesh.GetComponent <Identity>());
        }
        #region 初始化玩家网络
        if (!GameDataManager.OfflineMode)
        {
            if (_InstanceNetType == InstanceNetType.GameNetworkOthers)
            {
                PTC.enableUserInput = false;
                //PTC.AdvanceTrackSystem = false;
                PTC.OnlyReceiveControlActions = true;
            }
            if (_InstanceNetType == InstanceNetType.GameNetworkMaster)
            {
                PTC.enableUserInput = false;
            }

            if (_InstanceNetType == InstanceNetType.GameNetworkClient)
            {
                PTC.enableUserInput           = true;
                PTC.OnlyReceiveControlActions = true;
            }
            if (isBot(_InstanceNetType))
            {
                PTC.enableUserInput = false;
                if (_InstanceNetType == InstanceNetType.GameNetWorkBotClient)
                {
                    PTC.OnlyReceiveControlActions = true;
                }
                else
                {
                    PTC.OnlyReceiveControlActions = false;
                }
            }

            //_syncGroundVehicle = InstanceMesh.AddComponent<SyncGroundVehicle> ();
            //_syncGroundVehicle.tankInitSystem = this;
            //_syncGroundVehicle.Init (PTC, mt, _InstanceNetType);
        }
        #region 设置离线标识
        if (GameDataManager.OfflineMode)
        {
            if (_InstanceNetType == InstanceNetType.GameNetWorkOffline)
            {
                Identity MyIdentity = InstanceMesh.GetComponent <Identity> ();
                MyIdentity.Init(ownerTeam, InstanceNetType.GameNetWorkOffline);
                MyIdentity.SetOwnerInfo("LocalPlayer", VehicleName, 0);
            }
        }
        #endregion
        #endregion
        if (!isBot(_InstanceNetType))
        {
            StartCoroutine(PS.Init());
        }

        if (GameEventManager.onNewVehicleSpawned != null)
        {
            GameEventManager.onNewVehicleSpawned(this);
        }

        if (onVehicleLoaded != null)
        {
            onVehicleLoaded();
        }
    }
Пример #2
0
    TankInitSystem InitTankPrefabs()
    {
        #region Collision Detect
        if (vehicleData.modelData.MainModel.GetComponentsInChildren <BoxCollider>().Length == 0)
        {
            if (EditorUtility.DisplayDialog("Error", "Collision should be set.We will redirct you to it.", "OK,l will set it now!", "No,l will choose another one"))
            {
                EditorGUIUtility.PingObject(vehicleData.modelData.MainModel.GetInstanceID());
                OpenEditorScene();

                GameObject EditColliderInstance = Instantiate <GameObject>(vehicleData.modelData.MainModel);
                EditColliderInstance.name = vehicleData.modelData.MainModel.name;

                string TempPath       = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(vehicleData.modelData.MainModel.GetInstanceID()));
                string PrefabStoreDir = System.IO.Directory.CreateDirectory(string.Format(TempPath + "/Collisions_{0}", vehicleData.modelData.MainModel.name)).FullName;

                AssetDatabase.Refresh();
                return(null);
                //PrefabUtility.CreatePrefab (string.Format (PrefabStoreDir + "/{0}.prefab", vehicleData.modelData.MainModel.name),EditColliderInstance);
            }
            else
            {
                return(null);
            }
        }
        #endregion
        GameObject TankPrefabs  = new GameObject();
        GameObject InstanceMesh = Instantiate(vehicleData.modelData.MainModel);

        try {
            #region Init
            InstanceMesh.name = vehicleData.modelData.MainModel.name;
            #endregion

            TankPrefabs.name = InstanceMesh.name + "_Pre";

            InstanceMesh.transform.parent = TankPrefabs.transform;

            GameObject TankTransform = new GameObject("TankTransform");
            TankTransform.transform.parent = InstanceMesh.transform;

            Transform RightWheel, LeftWheel, RightUpperWheels, LeftUpperWheels, Turret, Gun, GunDym;
            #region Find Dumps
            RightWheel = InstanceMesh.transform.Find("RightWheel");
            LeftWheel  = InstanceMesh.transform.Find("LeftWheel");
            Turret     = InstanceMesh.transform.Find("Turret");
            Gun        = Turret.transform.Find("Gun");
            GunDym     = Gun.GetChild(0);


            RightUpperWheels = InstanceMesh.transform.Find("RightUpperWheel");
            LeftUpperWheels  = InstanceMesh.transform.Find("LeftUpperWheel");
            #endregion
            RightWheel.parent       = TankTransform.transform;
            LeftWheel.parent        = TankTransform.transform;
            LeftUpperWheels.parent  = TankTransform.transform;
            RightUpperWheels.parent = TankTransform.transform;


            GameObject TurretTransform = new GameObject("TurretTransform");
            GameObject GunTransform    = new GameObject("GunTransform");
            GameObject GunDymTransform = new GameObject("GunDym");

            VehicleComponentsReferenceManager referenceManager = InstanceMesh.AddComponent <VehicleComponentsReferenceManager>();

            TurretTransform.transform.SetParent(InstanceMesh.transform);
            TurretTransform.transform.position = Turret.transform.position;

            GunTransform.transform.position = Gun.transform.position;
            Turret.parent = TurretTransform.transform;

            Gun.parent = GunTransform.transform;
            GunTransform.transform.SetParent(TurretTransform.transform);

            GunDymTransform.transform.position = GunDym.transform.position;
            GunDymTransform.transform.SetParent(GunTransform.transform);
            GunDym.SetParent(GunDymTransform.transform);

            #region Add fire animation
            Animator FireAnimator = GunDymTransform.AddComponent <Animator>();
            FireAnimator.runtimeAnimatorController = vehicleData.vehicleTextData.TFParameter.GymAnimation;
            #endregion

            AddDumpNode("FFPoint", GunTransform.transform, true, referenceManager);
            AddDumpNode("EffectStart", GunTransform.transform, true, referenceManager);
            AddDumpNode("FireForceFeedbackPoint", GunTransform.transform, true, referenceManager);
            AddDumpNode("EngineSmoke", InstanceMesh.transform, true, referenceManager);
            AddDumpNode("EngineSound", InstanceMesh.transform, true, referenceManager);
            AddDumpNode("MainCameraFollowTarget", InstanceMesh.transform, true, referenceManager);
            AddDumpNode("MainCameraGunner", GunTransform.transform, true, referenceManager);
            AddDumpNode("MachineGunFFPoint", TurretTransform.transform, true, referenceManager);
            AddDumpNode("CenterOfGravity", InstanceMesh.transform, true, referenceManager);

            referenceManager.LeftTrack  = InstanceMesh.transform.Find("LeftTrack").gameObject;
            referenceManager.RightTrack = InstanceMesh.transform.Find("RightTrack").gameObject;

            GameObject HitBoxInstance = Instantiate <GameObject>(vehicleData.modelData.HitBox.HitBoxPrefab);
            HitBoxInstance.transform.Find("Main").name = "MainHitBox";
            HitBoxInstance.transform.Find("MainHitBox").SetParent(InstanceMesh.transform);

            HitBoxInstance.transform.Find("Turret").name = "TurretHitBox";
            HitBoxInstance.transform.Find("TurretHitBox").SetParent(TurretTransform.transform);

            HitBoxInstance.transform.Find("Gun").name = "GunHitBox";
            HitBoxInstance.transform.Find("GunHitBox").SetParent(GunTransform.transform);

            HitBoxInstance.transform.Find("Dym").name = "DymHitBox";
            HitBoxInstance.transform.Find("DymHitBox").SetParent(GunDymTransform.transform);

            DestroyImmediate(HitBoxInstance);
            Restore(LeftWheel.GetComponentsInChildren <Transform>());
            Restore(RightWheel.GetComponentsInChildren <Transform>());

            Restore(LeftUpperWheels.GetComponentsInChildren <Transform>());
            Restore(RightUpperWheels.GetComponentsInChildren <Transform>());

            new GameObject("MainCameraTarget").transform.SetParent(TurretTransform.transform);

            TankInitSystem initySystem = TankPrefabs.AddComponent <TankInitSystem>();
            initySystem.PSParameter  = vehicleData.vehicleTextData.PSParameter;
            initySystem.TFParameter  = vehicleData.vehicleTextData.TFParameter;
            initySystem.MTParameter  = vehicleData.vehicleTextData.MTParameter;
            initySystem.PTCParameter = vehicleData.vehicleTextData.PTCParameter;

            return(initySystem);
        }
        catch (System.Exception exception) {
            EditorUtility.DisplayDialog("Exception", exception.Message + "\n" + exception.StackTrace, "OK");
            DestroyImmediate(TankPrefabs);
            throw exception;
            return(null);
        }
    }
    public static TankInitSystem InitTankPrefabs(VehicleData vehicleData, bool isPreviewMode)
    {
        #region Collision Detect
        if (vehicleData.modelData.MainModel.GetComponentsInChildren <BoxCollider>().Length == 0)
        {
            if (EditorUtility.DisplayDialog("Error", "Collision should be set.We will redirct you to it.", "OK,l will set it now!", "No,l will choose another one"))
            {
                EditorGUIUtility.PingObject(vehicleData.modelData.MainModel.GetInstanceID());
                OpenEditorScene();

                GameObject EditColliderInstance = Instantiate <GameObject>(vehicleData.modelData.MainModel);
                EditColliderInstance.name = vehicleData.modelData.MainModel.name;

                string TempPath       = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(vehicleData.modelData.MainModel.GetInstanceID()));
                string PrefabStoreDir = System.IO.Directory.CreateDirectory(string.Format(TempPath + "/Collisions_{0}", vehicleData.modelData.MainModel.name)).FullName;

                AssetDatabase.Refresh();
                return(null);
                //PrefabUtility.CreatePrefab (string.Format (PrefabStoreDir + "/{0}.prefab", vehicleData.modelData.MainModel.name),EditColliderInstance);
            }
            else
            {
                return(null);
            }
        }
        #endregion
        GameObject TankPrefabs  = new GameObject();
        GameObject InstanceMesh = Instantiate(vehicleData.modelData.MainModel);

        try
        {
            #region Init
            InstanceMesh.name = vehicleData.vehicleTextData.AssetName;
            #endregion

            TankPrefabs.name = vehicleData.vehicleTextData.AssetName + "_Pre";

            InstanceMesh.transform.parent        = TankPrefabs.transform;
            InstanceMesh.transform.localPosition = vehicleData.cacheData.StartPoint;

            GameObject VehicleDynamics = new GameObject("VehicleDynamics");
            VehicleDynamics.transform.parent = InstanceMesh.transform;

            Transform RightWheel, LeftWheel, RightUpperWheels, LeftUpperWheels, Turret, Gun, GunDym;
            #region Find Dumps
            RightWheel = InstanceMesh.transform.Find("RightWheel");
            LeftWheel  = InstanceMesh.transform.Find("LeftWheel");
            Turret     = InstanceMesh.transform.Find("Turret");
            Gun        = Turret.transform.Find("Gun");
            GunDym     = Gun.GetChild(0);


            RightUpperWheels = InstanceMesh.transform.Find("RightUpperWheel");
            LeftUpperWheels  = InstanceMesh.transform.Find("LeftUpperWheel");
            #endregion
            RightWheel.parent       = VehicleDynamics.transform;
            LeftWheel.parent        = VehicleDynamics.transform;
            LeftUpperWheels.parent  = VehicleDynamics.transform;
            RightUpperWheels.parent = VehicleDynamics.transform;

            #region FireSystem
            VehicleComponentsReferenceManager referenceManager = InstanceMesh.AddComponent <VehicleComponentsReferenceManager>();

            var mutiTurretTransList = new List <FireSystemTransform>();

            //多炮塔配置
            foreach (var multiTurret in vehicleData.vehicleTextData.MultiFireSystemData)
            {
                var mTurret = InstanceMesh.transform.Find(multiTurret.turretPath);
                var mGun    = mTurret.Find(multiTurret.gunPath);
                var mDym    = mGun.Find(multiTurret.dymPath);

                var turretParent = mTurret.parent;

                CreateWrapper(mDym, true);
                CreateWrapper(mGun, true);
                CreateWrapper(mTurret, true);

                mDym.parent.SetParent(mGun.parent);
                mGun.parent.SetParent(mTurret.parent);
                mTurret.parent.SetParent(turretParent);

                var gunFollowerTrans = new GameObject("GunFollowerTrans").transform;
                gunFollowerTrans.SetParent(mGun.parent);
                gunFollowerTrans.localPosition = multiTurret.gunFollowerOffSet;


                var turretFollowerTrans = new GameObject("TurretFollowerTrans").transform;
                turretFollowerTrans.SetParent(mGun.parent);
                turretFollowerTrans.localPosition = multiTurret.turretFollowerOffSet;



                //多炮塔毕竟少数
                var mffpoint = new GameObject("FFPoint").transform;
                mffpoint.SetParent(mDym.parent);
                mffpoint.rotation = mDym.parent.rotation;
                mffpoint.position = mDym.parent.position + mDym.parent.forward * multiTurret.ffPointOffSet;

                var meffectStart = new GameObject("EffectStart").transform;
                meffectStart.SetParent(mDym.parent);
                meffectStart.rotation = mDym.parent.rotation;
                meffectStart.position = mDym.parent.position + mDym.parent.forward * multiTurret.effectStartOffSet;

                //但是 我们依然设置 Icon 方便编辑
                IconManager.SetIcon(mffpoint.gameObject, IconManager.LabelIcon.Red);
                IconManager.SetIcon(meffectStart.gameObject, IconManager.LabelIcon.Red);

                mutiTurretTransList.Add(
                    new FireSystemTransform
                {
                    turret           = mTurret.parent,
                    gun              = mGun.parent,
                    dym              = mDym.parent,
                    ffPoint          = mffpoint,
                    fireEffectPoint  = meffectStart,
                    gunFollowerTrans = gunFollowerTrans,
                });
            }
            referenceManager.mutiTurretTrans = mutiTurretTransList;

            GameObject TurretTransform = new GameObject("TurretTransform");
            GameObject GunTransform    = new GameObject("GunTransform");
            GameObject GunDymTransform = new GameObject("GunDym");


            TurretTransform.transform.SetParent(InstanceMesh.transform);
            TurretTransform.transform.position = Turret.transform.position;

            GunTransform.transform.position = Gun.transform.position;
            Turret.parent = TurretTransform.transform;

            Gun.parent = GunTransform.transform;
            GunTransform.transform.SetParent(TurretTransform.transform);

            GunDymTransform.transform.position = GunDym.transform.position;
            GunDymTransform.transform.SetParent(GunTransform.transform);
            GunDym.SetParent(GunDymTransform.transform);
            #endregion


            AddDumpNode("FFPoint", GunTransform.transform, vehicleData, true, referenceManager);
            AddDumpNode("EffectStart", GunTransform.transform, vehicleData, true, referenceManager);
            AddDumpNode("FireForceFeedbackPoint", GunTransform.transform, vehicleData, true, referenceManager);
            AddDumpNode("EngineSmoke", InstanceMesh.transform, vehicleData, true, referenceManager);
            AddDumpNode("EngineSound", InstanceMesh.transform, vehicleData, true, referenceManager);
            AddDumpNode("MainCameraFollowTarget", InstanceMesh.transform, vehicleData, true, referenceManager);
            AddDumpNode("MainCameraGunner", GunTransform.transform, vehicleData, true, referenceManager);
            AddDumpNode("CenterOfGravity", InstanceMesh.transform, vehicleData, true, referenceManager);
            AddDumpNode("BotPoint", InstanceMesh.transform, vehicleData, true, referenceManager);

            referenceManager.LeftTrack  = InstanceMesh.transform.Find("LeftTrack").gameObject;
            referenceManager.RightTrack = InstanceMesh.transform.Find("RightTrack").gameObject;

            referenceManager.LeftWheels  = ResortWheelTrans(GetChilds(LeftWheel));
            referenceManager.RightWheels = ResortWheelTrans(GetChilds(RightWheel));

            referenceManager.LeftUpperWheels  = ResortWheelTrans(GetChilds(LeftUpperWheels));
            referenceManager.RightUpperWheels = ResortWheelTrans(GetChilds(RightUpperWheels));

            GameObject HitBoxInstance = Instantiate <GameObject>(vehicleData.modelData.HitBox.HitBoxPrefab);
            HitBoxInstance.transform.position += vehicleData.cacheData.StartPoint;

            referenceManager.MainHitBox   = HitBoxInstance.transform.Find("Main").gameObject;
            referenceManager.TurretHitBox = HitBoxInstance.transform.Find("Turret").gameObject;
            referenceManager.GunHitBox    = HitBoxInstance.transform.Find("Gun").gameObject;
            referenceManager.DymHitBox    = HitBoxInstance.transform.Find("Dym").gameObject;

            HitBoxInstance.transform.Find("Main").name = "MainHitBox";
            HitBoxInstance.transform.Find("MainHitBox").SetParent(InstanceMesh.transform);

            HitBoxInstance.transform.Find("Turret").name = "TurretHitBox";
            HitBoxInstance.transform.Find("TurretHitBox").SetParent(TurretTransform.transform);

            HitBoxInstance.transform.Find("Gun").name = "GunHitBox";
            HitBoxInstance.transform.Find("GunHitBox").SetParent(GunTransform.transform);

            HitBoxInstance.transform.Find("Dym").name = "DymHitBox";
            HitBoxInstance.transform.Find("DymHitBox").SetParent(GunDymTransform.transform);

            DestroyImmediate(HitBoxInstance);



            new GameObject("MainCameraTarget").transform.SetParent(TurretTransform.transform);

            TankInitSystem initySystem = TankPrefabs.AddComponent <TankInitSystem>();
            initySystem.vehicleTextData = vehicleData.vehicleTextData;

            #region Vehicle Physic
            var vehiclePhysic = InstanceMesh.AddComponent <Rigidbody>();
            vehiclePhysic.mass             = vehicleData.vehicleTextData.PTCParameter.Mass;
            vehiclePhysic.drag             = 0.1f;
            vehiclePhysic.angularDrag      = 2.5f;
            vehiclePhysic.useGravity       = true;
            referenceManager.VehiclePhysic = vehiclePhysic;
            #endregion

            referenceManager.VehicleDynamics = VehicleDynamics;

            referenceManager.TurretGameObject = TurretTransform;
            referenceManager.GunGameObject    = GunTransform;
            referenceManager.GunDymGameObject = GunDymTransform;

            if (!isPreviewMode)
            {
                CreateWrapper(referenceManager.MainCameraFollowTarget.transform);
                CreateWrapper(referenceManager.MainCameraGunner.transform);
            }


            return(initySystem);
        }
        catch (System.Exception exception)
        {
            EditorUtility.DisplayDialog("Exception", exception.Message + "\n" + exception.StackTrace, "OK");
            DestroyImmediate(TankPrefabs);
            throw exception;
        }
    }
Пример #4
0
    private void AddDumpNode(string dumpName, Transform parent, bool isAddedToReference = false, VehicleComponentsReferenceManager referenceManager = null)
    {
        GameObject DumpNode = new GameObject(dumpName);

        DumpNode.transform.SetParent(parent);
        DumpNode.transform.localPosition    = ((VehicleObjectTransformData)vehicleData.cacheData.GetType().GetField(dumpName).GetValue(vehicleData.cacheData)).localPosition;
        DumpNode.transform.localEulerAngles = ((VehicleObjectTransformData)vehicleData.cacheData.GetType().GetField(dumpName).GetValue(vehicleData.cacheData)).localEulerAngle;
        IconManager.SetIcon(DumpNode, IconManager.LabelIcon.Orange);
        if (isAddedToReference)
        {
            referenceManager.GetType().GetField(dumpName).SetValue(referenceManager, DumpNode);
        }
    }
Пример #5
0
    TankInitSystem InitTankPrefabs()
    {
        #region 碰撞检测
        if (vehicleData.modelData.MainModel.GetComponentsInChildren <BoxCollider> ().Length == 0)
        {
            if (EditorUtility.DisplayDialog("错误", "坦克模型必须设置完毕碰撞数据", "好的,立刻去设置", "重新选择资源"))
            {
                EditorGUIUtility.PingObject(vehicleData.modelData.MainModel.GetInstanceID());
                OpenEditorScene();

                GameObject EditColliderInstance = Instantiate <GameObject> (vehicleData.modelData.MainModel);
                EditColliderInstance.name = vehicleData.modelData.MainModel.name;

                string TempPath       = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(vehicleData.modelData.MainModel.GetInstanceID()));
                string PrefabStoreDir = System.IO.Directory.CreateDirectory(string.Format(TempPath + "/Collisions_{0}", vehicleData.modelData.MainModel.name)).FullName;

                AssetDatabase.Refresh();
                //PrefabUtility.CreatePrefab (string.Format (PrefabStoreDir + "/{0}.prefab", vehicleData.modelData.MainModel.name),EditColliderInstance);
            }
            else
            {
                return(null);
            }
        }
        #endregion

        #region 初始化 坦克物体变量
        GameObject TankPrefabs  = new GameObject();
        GameObject InstanceMesh = Instantiate(vehicleData.modelData.MainModel);
        InstanceMesh.name = vehicleData.modelData.MainModel.name;
        #endregion

        TankPrefabs.name = InstanceMesh.name + "_Pre";

        InstanceMesh.transform.parent = TankPrefabs.transform;

        GameObject TankTransform = new GameObject("TankTransform");
        TankTransform.transform.parent = InstanceMesh.transform;

        Transform RightWheel, LeftWheel, RightUpperWheels, LeftUpperWheels, Turret, Gun, GunDym;
        #region 在模型上寻找虚拟对象
        RightWheel = InstanceMesh.transform.Find("RightWheel");
        LeftWheel  = InstanceMesh.transform.Find("LeftWheel");
        Turret     = InstanceMesh.transform.Find("Turret");
        Gun        = Turret.transform.Find("Gun");
        GunDym     = Gun.GetChild(0);

        RightUpperWheels = InstanceMesh.transform.Find("RightUpperWheel");
        LeftUpperWheels  = InstanceMesh.transform.Find("LeftUpperWheel");
        #endregion
        RightWheel.parent       = TankTransform.transform;
        LeftWheel.parent        = TankTransform.transform;
        LeftUpperWheels.parent  = TankTransform.transform;
        RightUpperWheels.parent = TankTransform.transform;

        #region 实例化坦克的附加物体 坦克控制器 坦克脚本 坦克ui 坦克瞄准镜 主摄像机 坦克引擎声音 坦克引擎烟雾 死亡效果
        //EngineSmoke = new GameObject("EngineSmoke");
        //LeftTrackEffect = new GameObject("LeftTrackEffect");
        //RightTrackEffect=new GameObject("RightTrackEffect");
        #endregion
        GameObject TurretTransform = new GameObject("TurretTransform");
        GameObject GunTransform    = new GameObject("GunTransform");
        GameObject GunDymTransform = new GameObject("GunDym");

        VehicleComponentsReferenceManager referenceManager = InstanceMesh.AddComponent <VehicleComponentsReferenceManager>();

        //GameObject MainHitBox = new GameObject ("MainHitBox");
        //GameObject TurretHitBox = new GameObject ("TurretHitBox");


        TurretTransform.transform.SetParent(InstanceMesh.transform);
        TurretTransform.transform.position = Turret.transform.position;

        GunTransform.transform.position = Gun.transform.position;
        Turret.parent = TurretTransform.transform;

        Gun.parent = GunTransform.transform;
        GunTransform.transform.SetParent(TurretTransform.transform);

        GunDymTransform.transform.position = GunDym.transform.position;
        GunDymTransform.transform.SetParent(GunTransform.transform);
        GunDym.SetParent(GunDymTransform.transform);

        #region 发射动画
        Animator FireAnimator = GunDymTransform.AddComponent <Animator> ();
        FireAnimator.runtimeAnimatorController = vehicleData.vehicleTextData.TFParameter.GymAnimation;
        #endregion

        AddDumpNode("FFPoint", GunTransform.transform, true, referenceManager);
        AddDumpNode("EffectStart", GunTransform.transform, true, referenceManager);
        AddDumpNode("FireForceFeedbackPoint", InstanceMesh.transform, true, referenceManager);
        AddDumpNode("EngineSmoke", InstanceMesh.transform, true, referenceManager);
        AddDumpNode("EngineSound", InstanceMesh.transform, true, referenceManager);
        AddDumpNode("MainCameraFollowTarget", InstanceMesh.transform, true, referenceManager);
        AddDumpNode("MainCameraGunner", GunTransform.transform, true, referenceManager);
        AddDumpNode("MachineGunFFPoint", TurretTransform.transform, true, referenceManager);
        AddDumpNode("CenterOfGravity", InstanceMesh.transform, true, referenceManager);



        //MainHitBox.transform.parent = TankModel.transform;
        //TurretHitBox.transform.parent = TurretTransform.transform;

        //EngineSmoke.transform.parent = TankModel.transform;
        //EngineSmoke.tag = "EngineSmoke";



        //		GameObject LODMesh = new GameObject ("LODMesh");
        //		LODMesh.transform.SetParent (TankModel.transform);
        //		LODMesh.tag = "LODMesh";

        GameObject HitBoxInstance = Instantiate <GameObject> (vehicleData.modelData.HitBox.HitBoxPrefab);
        HitBoxInstance.transform.Find("Main").name = "MainHitBox";
        HitBoxInstance.transform.Find("MainHitBox").SetParent(InstanceMesh.transform);

        HitBoxInstance.transform.Find("Turret").name = "TurretHitBox";
        HitBoxInstance.transform.Find("TurretHitBox").SetParent(TurretTransform.transform);

        HitBoxInstance.transform.Find("Gun").name = "GunHitBox";
        HitBoxInstance.transform.Find("GunHitBox").SetParent(GunTransform.transform);

        HitBoxInstance.transform.Find("Dym").name = "DymHitBox";
        HitBoxInstance.transform.Find("DymHitBox").SetParent(GunDymTransform.transform);

        DestroyImmediate(HitBoxInstance);
        //HitBoxInstance.transform.Find("Dym").SetParent()
        Restore(LeftWheel.GetComponentsInChildren <Transform> ());
        Restore(RightWheel.GetComponentsInChildren <Transform> ());

        Restore(LeftUpperWheels.GetComponentsInChildren <Transform> ());
        Restore(RightUpperWheels.GetComponentsInChildren <Transform> ());

        new GameObject("MainCameraTarget").transform.SetParent(TurretTransform.transform);

        TankInitSystem initySystem = TankPrefabs.AddComponent <TankInitSystem>();
        initySystem.PSParameter  = vehicleData.vehicleTextData.PSParameter;
        initySystem.TFParameter  = vehicleData.vehicleTextData.TFParameter;
        initySystem.MTParameter  = vehicleData.vehicleTextData.MTParameter;
        initySystem.PTCParameter = vehicleData.vehicleTextData.PTCParameter;

        return(initySystem);
    }