| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using HighlightingSystem;
- public class RayCastItem : MonoBehaviour
- {
- private Highlighter highlighter;
- /// <summary>
- /// 自身及父级列表
- /// </summary>
- private List<RayCastItem> parentList;
- /// <summary>
- /// 射线检测活跃状态
- /// </summary>
- private bool activeState = false;
- public bool ActiveState
- {
- get
- {
- return activeState;
- }
- set
- {
- if (activeState != value)
- {
- activeState = value;
- InitChildCollider();
- foreach (var item in childCollider)
- {
- item.gameObject.layer = activeState ? CameraRayCastManager.RayCastActivelayer : localLayer;
- }
- }
- }
- }
- public bool HighlighterConstant
- {
- get
- {
- if (highlighter == null)
- return false;
- else
- return highlighter.constant;
- }
- }
- private int localLayer;
- private Collider[] childCollider;
- private void Awake()
- {
- highlighter = this.GetComponent<Highlighter>() == null
- ? this.gameObject.AddComponent<Highlighter>()
- : this.GetComponent<Highlighter>();
- highlighter.overlay = true;
- localLayer = this.gameObject.layer;
- }
- private void InitChildCollider()
- {
- if (childCollider == null)
- {
- childCollider = this.GetComponentsInChildren<Collider>();
- }
- }
- public void InitRayCastParents()
- {
- if (parentList == null)
- {
- CreatParentLists();
- }
- }
- public void CreatParentLists()
- {
- parentList = new List<RayCastItem>();
- parentList.AddRange(this.transform.GetComponentsInParent<RayCastItem>());
- }
- public bool Contains(RayCastItem item)
- {
- if (parentList == null)
- {
- return this == item;
- }
- return parentList.Contains(item);
- }
- public void OpenHighlighter(Color color)
- {
- foreach (var item in this.GetComponentsInChildren<Highlighter>())
- {
- item.ConstantOn(color, 0);
- }
- }
- public void CloseHighlighter()
- {
- foreach (var item in this.GetComponentsInChildren<Highlighter>())
- {
- item.ConstantOff(0);
- }
- }
- public void OpenHighlighterFlashing()
- {
- foreach (var item in this.GetComponentsInChildren<Highlighter>())
- {
- //item.FlashingOn(Color.blue, Color.cyan, 2);
- item.tweenDuration = 0.3f;
- item.tweenRepeatCount = 2; //闪烁次数
- item.tweenGradient.SetKeys(
- new GradientColorKey[]
- {
- new GradientColorKey { color=new Color(1,0,0),time=0.5f},
- new GradientColorKey { color=new Color(0,1,0),time=1.5f}
- },
- new GradientAlphaKey[]
- {
- new GradientAlphaKey{alpha=255},
- new GradientAlphaKey{alpha=255,time=1.5f}
- });
- item.TweenStart();//闪烁
- }
- }
- public void CloseHighlighterFlashing()
- {
- foreach (var item in this.GetComponentsInChildren<Highlighter>())
- {
- //item.FlashingOff();
- item.TweenStop();
- }
- }
- }
|