首页 > 其他分享 >Unity实现物件选中效果

Unity实现物件选中效果

时间:2023-03-30 21:38:10浏览次数:42  
标签:false void private 物件 counterFocused Unity 选中 return null


首先制作两个显示效果,一个是未被选中时的物件(Visual),一个是被选中时的物件(Focused)。
然后将这两个gameObject挂载到父节点上。创建C#代码CounterFocused添加为父节点的组件。

public class CounterFocused : MonoBehaviour
{
    [SerializeField]
    private GameObject NormalVisual;

    [SerializeField]
    private GameObject FocusedVisual;

    private void Start()
    {
        Debug.Assert(NormalVisual!= null);
        Debug.Assert(FocusedVisual!= null);

        DeFocus();
    }

    public void Focus()
    {
        NormalVisual.SetActive(false);
        FocusedVisual.SetActive(true);
    }

    public void DeFocus()
    {
        NormalVisual.SetActive(true);
        FocusedVisual.SetActive(false);
    }

}


然后将两个子节点拖入两个变量中。

在Player脚本中每帧检测角色对面是否有可以选中的物件。

    private void Update()
    {
        HandleMovement();

        TryFocusObj();
    }

    private void TryFocusObj()
    {
        if(IsFocusedObjSameAsOldOne(out CounterFocused counterFocused))
            return;

        ResetCounterFocused();

        if(counterFocused!= null)
        {
            this._counterFocused = counterFocused;
            this._counterFocused.Focus();
        }
    }

    private bool IsFocusedObjSameAsOldOne(out CounterFocused counterFocusedOut)
    {
        if (!Physics.Raycast(transform.position, transform.forward, out RaycastHit raycastHit, 2f))
        {
            counterFocusedOut = null;
            return false;
        }

        if (!raycastHit.transform.TryGetComponent(out CounterFocused counterFocused))
        {
            counterFocusedOut = null;
            return false;
        }

        counterFocusedOut = counterFocused;

        if (_counterFocused == null)
            return false;

        if (!_counterFocused.Equals(counterFocused))
            return false;

        return true;
    }

    private void ResetCounterFocused()
    {
        if(_counterFocused != null) 
            this._counterFocused.DeFocus();

        this._counterFocused = null;
    }

标签:false,void,private,物件,counterFocused,Unity,选中,return,null
From: https://www.cnblogs.com/dewxin/p/17274229.html

相关文章

  • unity [数学] 四元数和Vector3相乘的意义
    参考:https://answers.unity.com/questions/186252/multiply-quaternion-by-vector.html 总结:Quaternion*Vector3表示在世界坐标系下,Vector3的任意旋转; Inthequaternionworld,multiplicationisthewaytoapplytherotationtosomething  【在Quaternion下,相......
  • unity中Dictionary用法
    作用在unity中序列化显示,每个key只能对应一个值,对应关系线性实例化Dictionary<key,value>字典名Dic;可嵌套Dictionaty<key1,Dictionary<key2,value>Dic>Dict1;常见方......
  • Unity中基于EPPLUS的Excel转换以及Json数据读取
    摘要:主要使用Epplus的的表格转换功能,将表格转换成Json,而Unity中Json的使用要搭配对应结构的类,故顺便将利用表格结构生成对应的类,免除人工创建的麻烦过程。示例:表格示例......
  • 运用unity做一个实现简单AR功能的手机APP
    首先前往unity官网:https://unity.cn/releases,下拉找到下载UnityHub绿色按钮下载完安装进入UnityHub中,在侧边栏找到Installs,点击打开下载如图所示的unity版本(注:unity......
  • 去掉input选中框时的黑边
    input搜索框选择时会出现黑色的边框原因:只是因为input选择框有默认的值解决方法:通过修改input的css属性即可;因为是所有样式都会存在的问题所以放在了一个公共的css文件......
  • Unity触碰函数OnTriggerStay与Input.GetKeyDown
    (写写博客尝尝鲜)触碰函数中不要使用按键检测我想让玩家在某个区域内进行按键检测,代码如下1voidTriggerPlayerStay(Collider2Dcollider)2{3if(co......
  • Unity
    真的牛逼GPT,效率拉满......
  • 创建桌面快捷方式 Add items to Ubuntu 12.04 Unity Launcher (quicklaunch)
    AdditemstoUbuntu12.04UnityLauncher(quicklaunch)TherecentupgradetoUbuntu12.04PrecisePangolinleftmesomewhathangingwhenitcomestocreatinglau......
  • azure databricks中使用Unity Catalog 03--Data Sharing
    本文介绍AzureDatabricks中的DeltaSharing,这是安全的数据共享平台,可用于与组织外的用户共享AzureDatabricks中的数据。sharing分两类:开放共享:可与任何用户共享数据......
  • Unity Shader案例04-------透明
    Shader"CLF/SetTransparent"{Properties{_Diffuse("Diffuse",Color)=(1,1,1,1)//漫反射_MainTex("MainTex",2D)="white"{}//2......