You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.3 KiB
93 lines
3.3 KiB
3 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using HTC.UnityPlugin.ColliderEvent;
|
||
|
using HTC.UnityPlugin.Utility;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
//每种配件有且只有一个,安装之后枪上原有的配件会自动返还到背包、手里、或者枪柜里
|
||
|
//这个类为按钮类,按下按钮生成对应物品,或者返还对应物品
|
||
|
public class GunItemButton : MonoBehaviour ,IColliderEventPressDownHandler{
|
||
|
//type用整型表示,来确定配件的类型,相同位置的配件会进行替换
|
||
|
public GameObject self_;
|
||
|
private GameObject selfObj_;
|
||
|
private Transform leftHand_;
|
||
|
private Button selfBtn_;
|
||
|
//可以被枪所适配(每把枪有一个适配数组,如果数组里面没有该id则从列表中清除)
|
||
|
public int id_;
|
||
|
//表明在某一类配件栏里
|
||
|
public int type_;
|
||
|
//装配位置
|
||
|
private Transform transform_;
|
||
|
//取消射线选择用
|
||
|
private GameObject leftRay;
|
||
|
//配件是否在手里
|
||
|
public bool isInHand;
|
||
|
//配件是否在枪上
|
||
|
public bool isInGun;
|
||
|
//邦定玩家控制类
|
||
|
private PlayerControl player;
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
transform_ = GameObject.FindGameObjectWithTag("gun").transform;
|
||
|
selfBtn_ = gameObject.GetComponent<Button>();
|
||
|
leftHand_ = GameObject.FindGameObjectWithTag("leftHand").transform;
|
||
|
leftRay = GameObject.FindGameObjectWithTag("leftRay");
|
||
|
player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerControl>();
|
||
|
}
|
||
|
|
||
|
//将地上的配件或者物品栏里的配件转移到手上(btn禁用)
|
||
|
public void GetItem() {
|
||
|
isInHand = true;
|
||
|
selfObj_ = Instantiate(self_, leftHand_);
|
||
|
selfObj_.AddComponent<GunItem>();
|
||
|
selfObj_.GetComponent<GunItem>().itemBtn = selfBtn_;
|
||
|
selfObj_.transform.localPosition = new Vector3(0,-0.1f,0);
|
||
|
selfBtn_.interactable = false;
|
||
|
leftRay.SetActive(false);
|
||
|
player.leftTriggerClick.AddListener(ReleaseItem);
|
||
|
}
|
||
|
|
||
|
//安装配件失败时,配件会返回仓库(btn恢复禁用)
|
||
|
public void ReleaseItem() {
|
||
|
ReturnItem();
|
||
|
}
|
||
|
|
||
|
//配件安装成功时调用的方法(如果轨道上有配件,将枪上原有的配件与手中的配件进行交换)
|
||
|
public void InstallItem(Vector3 pos){
|
||
|
isInHand = false;
|
||
|
selfObj_.transform.SetParent(transform_);
|
||
|
selfObj_.transform.localPosition = pos;
|
||
|
selfObj_.transform.localEulerAngles = self_.transform.localEulerAngles;
|
||
|
leftRay.SetActive(true);
|
||
|
player.leftTriggerClick.RemoveAllListeners();
|
||
|
}
|
||
|
|
||
|
//配件的返还(如果配件在手上,可以通过这个方法把手里的配件放回到背包里)
|
||
|
public void ReturnItem() {
|
||
|
isInHand = false;
|
||
|
Destroy(selfObj_);
|
||
|
selfBtn_.interactable = true;
|
||
|
leftRay.SetActive(true);
|
||
|
player.leftTriggerClick.RemoveAllListeners();
|
||
|
}
|
||
|
|
||
|
//配件的更替(如果枪上有配件,进行安装配件的时候,会把枪上的配件与手上的配件进行更替)
|
||
|
public void ReplaceItem() {
|
||
|
Destroy(selfObj_);
|
||
|
GetItem();
|
||
|
}
|
||
|
|
||
|
//配件被点击的事件
|
||
|
public void OnColliderEventPressDown(ColliderButtonEventData eventData)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
//获取枪械坐标
|
||
|
public Transform getGunTrans() {
|
||
|
return transform_;
|
||
|
}
|
||
|
}
|