123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System.Collections.Generic;
- using UnityEngine;
- public class WeaponPartView : MonoBehaviour
- {
- public WeaponPartDesc[] parts;
- public Material hologramMat;
- public GameObject oldScope;
- private Dictionary<int, Material> oldMatDict = new Dictionary<int, Material>();
- /// <summary>
- /// 是否需要在分组测试中区分处理
- /// </summary>
- [SerializeField]
- private WeaponShowExt m_weaponShowPosition;
- private void Start()
- {
- if (m_weaponShowPosition.isNeedSet)
- {
- transform.GetChild(0).localPosition = m_weaponShowPosition.newPosition;//修正子物体的位置
- transform.localRotation = Quaternion.Euler(m_weaponShowPosition.newRotation);//修正根节点的旋转
- }
- //InitPart();
- }
- public Vector3 GenerateUnitVector(Vector3 vector)
- {
- Vector3 unitVector = vector.normalized;
- Debug.Log($"Generated unit vector: {unitVector}");
- return unitVector;
- }
- private void InitPart()
- {
- for (int i = 0; i < parts.Length; ++i)
- {
- parts[i].weaponPart.SetActive(false);
- if (!oldMatDict.ContainsKey(parts[i].partType))
- {
- MeshRenderer mr = parts[i].weaponPart.GetComponent<MeshRenderer>();
- if (mr == null)
- {
- oldMatDict.Add(parts[i].partType, null);
- }
- else
- {
- oldMatDict.Add(parts[i].partType, mr.sharedMaterial);
- }
- }
- }
- }
- public void Show(int weaponID)
- {
- InitPart();
- List<int> partTypeList = new List<int>();//可以显示的枪的部件类型
- int hologramPartType = 0;//要显示全息投影的枪的部件
- int curWeaponLv = SSTRGame.PlayerRuntimeData.Instance.GetWeaponAttributeSumLevel(weaponID);
- var weaponParts = DataManager.Instance.csvData.WeaponPart;
- for (int i = 0; i < weaponParts.Count; ++i)
- {
- if (weaponParts[i].weaponID == weaponID)
- {
- if (curWeaponLv >= weaponParts[i].equipLevel)
- {
- partTypeList.Add(weaponParts[i].partType);
- }
- if (curWeaponLv + 1 == weaponParts[i].equipLevel)
- {
- hologramPartType = weaponParts[i].partType;
- }
- }
- }
- for (int i = 0; i < parts.Length; ++i)
- {
- MeshRenderer mr = parts[i].weaponPart.GetComponent<MeshRenderer>();
- if (partTypeList.Contains(parts[i].partType))
- {
- Material commonMat = oldMatDict[parts[i].partType];
- if (mr != null && commonMat != null && commonMat.name != mr.sharedMaterial.name)
- {
- mr.sharedMaterial = commonMat;
- }
- parts[i].weaponPart.SetActive(true);
- }
- if (parts[i].partType == hologramPartType && mr != null)
- {
- mr.material = hologramMat;
- parts[i].weaponPart.SetActive(true);
- if (parts[i].partType == (int)ArmCtrl.WeaponPartType.Scope)
- {
- if (oldScope != null)
- {
- oldScope.SetActive(false);
- }
- }
- }
- }
- }
- /// <summary>
- /// 武器展示位置
- /// </summary>
- [System.Serializable]
- private struct WeaponShowExt
- {
- /// <summary>
- /// 是否需要更改
- /// </summary>
- public bool isNeedSet;
- /// <summary>
- /// 新的坐标
- /// </summary>
- public Vector3 newPosition;
- /// <summary>
- /// 新的旋转值
- /// </summary>
- public Vector3 newRotation;
- }
- }
- [System.Serializable]
- public struct WeaponPartDesc
- {
- public int partType;
- public GameObject weaponPart;
- }
|