WeaponPartView.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class WeaponPartView : MonoBehaviour
  4. {
  5. public WeaponPartDesc[] parts;
  6. public Material hologramMat;
  7. public GameObject oldScope;
  8. private Dictionary<int, Material> oldMatDict = new Dictionary<int, Material>();
  9. /// <summary>
  10. /// 是否需要在分组测试中区分处理
  11. /// </summary>
  12. [SerializeField]
  13. private WeaponShowExt m_weaponShowPosition;
  14. private void Start()
  15. {
  16. if (m_weaponShowPosition.isNeedSet)
  17. {
  18. transform.GetChild(0).localPosition = m_weaponShowPosition.newPosition;//修正子物体的位置
  19. transform.localRotation = Quaternion.Euler(m_weaponShowPosition.newRotation);//修正根节点的旋转
  20. }
  21. //InitPart();
  22. }
  23. public Vector3 GenerateUnitVector(Vector3 vector)
  24. {
  25. Vector3 unitVector = vector.normalized;
  26. Debug.Log($"Generated unit vector: {unitVector}");
  27. return unitVector;
  28. }
  29. private void InitPart()
  30. {
  31. for (int i = 0; i < parts.Length; ++i)
  32. {
  33. parts[i].weaponPart.SetActive(false);
  34. if (!oldMatDict.ContainsKey(parts[i].partType))
  35. {
  36. MeshRenderer mr = parts[i].weaponPart.GetComponent<MeshRenderer>();
  37. if (mr == null)
  38. {
  39. oldMatDict.Add(parts[i].partType, null);
  40. }
  41. else
  42. {
  43. oldMatDict.Add(parts[i].partType, mr.sharedMaterial);
  44. }
  45. }
  46. }
  47. }
  48. public void Show(int weaponID)
  49. {
  50. InitPart();
  51. List<int> partTypeList = new List<int>();//可以显示的枪的部件类型
  52. int hologramPartType = 0;//要显示全息投影的枪的部件
  53. int curWeaponLv = SSTRGame.PlayerRuntimeData.Instance.GetWeaponAttributeSumLevel(weaponID);
  54. var weaponParts = DataManager.Instance.csvData.WeaponPart;
  55. for (int i = 0; i < weaponParts.Count; ++i)
  56. {
  57. if (weaponParts[i].weaponID == weaponID)
  58. {
  59. if (curWeaponLv >= weaponParts[i].equipLevel)
  60. {
  61. partTypeList.Add(weaponParts[i].partType);
  62. }
  63. if (curWeaponLv + 1 == weaponParts[i].equipLevel)
  64. {
  65. hologramPartType = weaponParts[i].partType;
  66. }
  67. }
  68. }
  69. for (int i = 0; i < parts.Length; ++i)
  70. {
  71. MeshRenderer mr = parts[i].weaponPart.GetComponent<MeshRenderer>();
  72. if (partTypeList.Contains(parts[i].partType))
  73. {
  74. Material commonMat = oldMatDict[parts[i].partType];
  75. if (mr != null && commonMat != null && commonMat.name != mr.sharedMaterial.name)
  76. {
  77. mr.sharedMaterial = commonMat;
  78. }
  79. parts[i].weaponPart.SetActive(true);
  80. }
  81. if (parts[i].partType == hologramPartType && mr != null)
  82. {
  83. mr.material = hologramMat;
  84. parts[i].weaponPart.SetActive(true);
  85. if (parts[i].partType == (int)ArmCtrl.WeaponPartType.Scope)
  86. {
  87. if (oldScope != null)
  88. {
  89. oldScope.SetActive(false);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// 武器展示位置
  97. /// </summary>
  98. [System.Serializable]
  99. private struct WeaponShowExt
  100. {
  101. /// <summary>
  102. /// 是否需要更改
  103. /// </summary>
  104. public bool isNeedSet;
  105. /// <summary>
  106. /// 新的坐标
  107. /// </summary>
  108. public Vector3 newPosition;
  109. /// <summary>
  110. /// 新的旋转值
  111. /// </summary>
  112. public Vector3 newRotation;
  113. }
  114. }
  115. [System.Serializable]
  116. public struct WeaponPartDesc
  117. {
  118. public int partType;
  119. public GameObject weaponPart;
  120. }