void Replace() { //instantiate prefabs TempPlayerPrefab = PrefabUtility.InstantiatePrefab(PlayerPrefab.gameObject) as GameObject; TempPlayerModel = PrefabUtility.InstantiatePrefab(PlayerModel.gameObject) as GameObject; //change name of prefabs to identify TempPlayerPrefab.gameObject.name += " [NEW]"; TempPlayerModel.name += " [NEW]"; // get the current player model GameObject RemoteChildPlayer = TempPlayerPrefab.GetComponentInChildren <bl_PlayerAnimations>().gameObject; GameObject ActualModel = TempPlayerPrefab.GetComponentInChildren <bl_HeatLookMecanim>().gameObject; Transform NetGunns = TempPlayerPrefab.GetComponent <bl_PlayerSync>().NetworkGuns[0].transform.parent; //set the new model to the same position as the current model TempPlayerModel.transform.parent = RemoteChildPlayer.transform; TempPlayerModel.transform.localPosition = ActualModel.transform.localPosition; TempPlayerModel.transform.localRotation = ActualModel.transform.localRotation; //add and copy components of actual player model bl_HeatLookMecanim ahl = ActualModel.GetComponent <bl_HeatLookMecanim>(); if (TempPlayerModel.GetComponent <Animator>() == null) { TempPlayerModel.AddComponent <Animator>(); } Animator NewAnimator = TempPlayerModel.GetComponent <Animator>(); RightHand = NewAnimator.GetBoneTransform(HumanBodyBones.RightHand); if (RightHand == null) { ErrorLine = "Can't get right hand from new model, are u sure that is a humanoid rig?"; return; } if (ahl != null) { bl_HeatLookMecanim newht = TempPlayerModel.AddComponent <bl_HeatLookMecanim>(); newht.Target = ahl.Target; newht.Body = ahl.Body; newht.Weight = ahl.Weight; newht.Head = ahl.Head; newht.Lerp = ahl.Lerp; newht.Eyes = ahl.Eyes; newht.Clamp = ahl.Clamp; // newht.Offset = ahl.Offset; Animator oldAnimator = ActualModel.GetComponent <Animator>(); NewAnimator.runtimeAnimatorController = oldAnimator.runtimeAnimatorController; NewAnimator.applyRootMotion = oldAnimator.hasRootMotion; } bl_PlayerAnimations pa = TempPlayerPrefab.transform.GetComponentInChildren <bl_PlayerAnimations>(); bl_BodyPartManager bdm = TempPlayerPrefab.transform.GetComponentInChildren <bl_BodyPartManager>(); pa.m_animator = NewAnimator; bdm.m_Animator = NewAnimator; bdm.mRigidBody.Clear(); bdm.HitBoxs.Clear(); bdm.mRigidBody.AddRange(GetRigidBodys(TempPlayerModel.transform)); Collider[] allColliders = GetCollider(TempPlayerModel.transform); if (allColliders == null || allColliders.Length <= 0) { ErrorLine = "New player model prefab is not rag-dolled, to continue please create a rag-doll of it."; return; } foreach (Collider c in allColliders) { if (c.gameObject.tag != bl_BodyPartManager.HitBoxTag) { c.gameObject.tag = bl_BodyPartManager.HitBoxTag; } bl_BodyPartManager.Part p = new bl_BodyPartManager.Part(); p.m_Collider = c; p.name = c.name; if (c.gameObject.name.ToLower() == "head") { p.m_HeatShot = true; p.m_Multipler = 50; } bdm.HitBoxs.Add(p); } bdm.AddScript(); NetGunns.parent = RightHand; NetGunns.localPosition = Vector3.zero; NetGunns.rotation = RightHand.rotation; ActualModel.name += " (DELETE)"; ActualModel.SetActive(false); var view = (SceneView)SceneView.sceneViews[0]; view.AlignViewToObject(TempPlayerPrefab.transform); view.ShowNotification(new GUIContent("Player Model Replace!")); done = true; maxSize = new Vector2(400, 50); }
void SetUpModelInPrefab() { #if UNITY_2018_3_OR_NEWER PrefabUtility.UnpackPrefabInstance(PlayerModel, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); #endif GameObject TempPlayerPrefab = PlayerInstantiated; GameObject TempPlayerModel = PlayerModel; //change name of prefabs to identify PlayerInstantiated.gameObject.name += " [NEW]"; PlayerModel.name += " [NEW]"; // get the current player model GameObject RemoteChildPlayer = TempPlayerPrefab.GetComponentInChildren <bl_PlayerAnimations>().gameObject; GameObject ActualModel = TempPlayerPrefab.GetComponentInChildren <bl_PlayerIK>().gameObject; Transform NetGunns = TempPlayerPrefab.GetComponent <bl_PlayerSync>().NetworkGuns[0].transform.parent; //set the new model to the same position as the current model TempPlayerModel.transform.parent = RemoteChildPlayer.transform; TempPlayerModel.transform.localPosition = ActualModel.transform.localPosition; TempPlayerModel.transform.localRotation = ActualModel.transform.localRotation; //add and copy components of actual player model bl_PlayerIK ahl = ActualModel.GetComponent <bl_PlayerIK>(); if (TempPlayerModel.GetComponent <Animator>() == null) { TempPlayerModel.AddComponent <Animator>(); } Animator NewAnimator = TempPlayerModel.GetComponent <Animator>(); if (ahl != null) { bl_PlayerIK newht = TempPlayerModel.AddComponent <bl_PlayerIK>(); newht.Target = ahl.Target; newht.Body = ahl.Body; newht.Weight = ahl.Weight; newht.Head = ahl.Head; newht.Lerp = ahl.Lerp; newht.Eyes = ahl.Eyes; newht.Clamp = ahl.Clamp; newht.useFootPlacement = ahl.useFootPlacement; newht.FootDownOffset = ahl.FootDownOffset; newht.FootHeight = ahl.FootHeight; newht.FootLayers = ahl.FootLayers; newht.PositionWeight = ahl.PositionWeight; newht.RotationWeight = ahl.RotationWeight; newht.AimSightPosition = ahl.AimSightPosition; newht.HandOffset = ahl.HandOffset; newht.TerrainOffset = ahl.TerrainOffset; newht.Radious = ahl.Radious; Animator oldAnimator = ActualModel.GetComponent <Animator>(); NewAnimator.runtimeAnimatorController = oldAnimator.runtimeAnimatorController; NewAnimator.applyRootMotion = oldAnimator.hasRootMotion; if (NewAnimator.avatar == null) { NewAnimator.avatar = oldAnimator.avatar; Debug.LogWarning("Your new model doesn't have a avatar, that can cause some problems with the animations, be sure to add it manually."); } } Transform RightHand = NewAnimator.GetBoneTransform(HumanBodyBones.RightHand); if (RightHand == null) { Debug.Log("Can't get right hand from new model, are u sure that is an humanoid rig?"); return; } bl_PlayerAnimations pa = TempPlayerPrefab.transform.GetComponentInChildren <bl_PlayerAnimations>(); bl_BodyPartManager bdm = TempPlayerPrefab.transform.GetComponentInChildren <bl_BodyPartManager>(); pa.m_animator = NewAnimator; bdm.m_Animator = NewAnimator; bdm.GetRequireBones(); bdm.mRigidBody.Clear(); bdm.HitBoxs.Clear(); bdm.mRigidBody.AddRange(GetRigidBodys(TempPlayerModel.transform)); Collider[] allColliders = GetCollider(TempPlayerModel.transform); if (allColliders == null || allColliders.Length <= 0) { Debug.Log("New player model prefab is not rag-dolled, to continue please create a rag-doll of it."); return; } foreach (Collider c in allColliders) { if (c.gameObject.tag != bl_BodyPartManager.HitBoxTag) { c.gameObject.tag = bl_BodyPartManager.HitBoxTag; } bl_BodyPartManager.Part p = new bl_BodyPartManager.Part(); p.m_Collider = c; p.name = c.name; c.gameObject.layer = LayerMask.NameToLayer("Player"); if (c.gameObject.name.ToLower().Contains("head")) { p.m_HeatShot = true; p.m_Multipler = 50; } bdm.HitBoxs.Add(p); } bdm.AddScript(); if (RightHand != null) { NetGunns.parent = RightHand; NetGunns.localPosition = Vector3.zero; NetGunns.rotation = RightHand.rotation; } else { Debug.Log("Can't find right hand"); } ActualModel.name += " (DELETE THIS)"; ActualModel.SetActive(false); var view = (SceneView)SceneView.sceneViews[0]; view.LookAt(ActualModel.transform.position); view.ShowNotification(new GUIContent("Player Setup")); }