WeaponController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Add this script to an empty GameObject, then put your weapon model as a child of this GameObject and assign it to "Weapon Model" slot.
  6. /// </summary>
  7. namespace SoftKitty.MasterCharacterCreator
  8. {
  9. [System.Serializable]
  10. public class WeaponPositionData
  11. {
  12. public string CarryParentTransform;
  13. public string HoldParentTransform;
  14. public bool VisibleWhenCarry = true;
  15. public Vector3 [] Pos=new Vector3[4];
  16. public Vector3 [] Rot = new Vector3[4];
  17. public Vector3 [] Scale = new Vector3[4] { new Vector3(1F,1F,1F), new Vector3(1F, 1F, 1F) , new Vector3(1F, 1F, 1F) , new Vector3(1F, 1F, 1F) };
  18. }
  19. public class WeaponController : MonoBehaviour
  20. {
  21. public string uid;
  22. public WeaponType Type;
  23. public Transform SheathModel;
  24. public Transform WeaponModel;
  25. public WeaponPositionData Data=new WeaponPositionData();
  26. public bool PositionMode = false;
  27. public GameObject PreviewModel;
  28. public Vector3 OriginalPos;
  29. public Quaternion OriginalRot;
  30. public Vector3 OriginalScale;
  31. public Transform OriginalParent;
  32. public Sex PreviewSex = Sex.Male;
  33. public bool PreviewHold = false;
  34. public bool PreviewCarry = false;
  35. private GameObject SheathModelClone;
  36. void Awake()
  37. {
  38. if (PreviewModel != null) Destroy(PreviewModel);
  39. PositionMode = false;
  40. }
  41. private void OnDrawGizmos()
  42. {
  43. if (!Application.isPlaying && PositionMode && PreviewModel!=null)
  44. {
  45. if (PreviewHold)
  46. {
  47. Transform _targetBone = FindChildTransform(PreviewModel.transform, Data.HoldParentTransform);
  48. if (_targetBone != null)
  49. {
  50. Gizmos.color = new Color(0F,1F,0F,0.2F);
  51. Gizmos.DrawSphere(_targetBone.position,0.1F);
  52. }
  53. }
  54. if (PreviewCarry)
  55. {
  56. Transform _targetBone = FindChildTransform(PreviewModel.transform, Data.CarryParentTransform);
  57. if (_targetBone != null)
  58. {
  59. Gizmos.color = new Color(0F, 1F, 0F, 0.2F);
  60. Gizmos.DrawSphere(_targetBone.position, 0.1F);
  61. }
  62. }
  63. }
  64. }
  65. public void RandomUid()//Assigns a random unique ID for this character.
  66. {
  67. List<byte> _bytes = new List<byte>();
  68. for (int i = 0; i < Random.Range(6, 8); i++)
  69. {
  70. _bytes.Add((byte)Random.Range(63, 123));
  71. }
  72. uid = System.Text.Encoding.ASCII.GetString(_bytes.ToArray());
  73. }
  74. public void SetSheath(bool _hold, CharacterEntity _entity)
  75. {
  76. if (SheathModel != null)
  77. {
  78. SheathModel.gameObject.SetActive(!_hold);
  79. if (_hold && _entity.GetBoneByName(Data.CarryParentTransform)!=null)
  80. {
  81. if (SheathModelClone == null)
  82. {
  83. SheathModelClone = new GameObject(gameObject.name+ "_SheathClone");
  84. GameObject _newObj = Instantiate(SheathModel.gameObject, SheathModelClone.transform);
  85. _newObj.transform.localPosition = SheathModel.localPosition;
  86. _newObj.transform.localRotation = SheathModel.localRotation;
  87. _newObj.transform.localScale = SheathModel.localScale;
  88. SheathModelClone.transform.SetParent(_entity.GetBoneByName(Data.CarryParentTransform));
  89. int _id = ((int)_entity.mCharacterAppearance._Sex) * 2 + 1;
  90. SheathModelClone.transform.localPosition = Data.Pos[_id];
  91. SheathModelClone.transform.localEulerAngles = Data.Rot[_id];
  92. SheathModelClone.transform.localScale = Data.Scale[_id];
  93. _newObj.SetActive(true);
  94. }
  95. }
  96. if (SheathModelClone != null) SheathModelClone.SetActive(_hold);
  97. }
  98. }
  99. public void Unequip() {
  100. if (SheathModelClone != null) Destroy(SheathModelClone);
  101. Destroy(gameObject);
  102. }
  103. private Transform FindChildTransform(Transform _trans, string _name)
  104. {
  105. if (_name == "") return null;
  106. foreach (var obj in _trans.GetComponentsInChildren<Transform>(true))
  107. {
  108. if (obj.name == _name) return obj;
  109. }
  110. return null;
  111. }
  112. }
  113. }