URP-UGUI交互功能实现

一、非代码层面实现交互(SetActive)

Button :在OnClick()中添加SetActive方法(但是此时只首次有效)

Toggle:在OnClick()中添加动态的SetActive方法

(给开关Toggle添加对目标对象的SetActive方法,此时有动态和静态两种选项。选择动态时,对象的显示与隐藏跟随开关的状态而改变)

二、代码层面交互(滑动条控制旋转速度、按钮改变颜色)

1. 利用Slider滑动条改变物体的旋转速度(实则改变旋转角度)

补充旋转代码知识:

(1)首先创建一个Rotate脚本,拖动到用于旋转的对象上,在Rotate脚本中的Upadate方法中添 加this.transform.Rotate(Vector3.up,Angle*Time.deltaTime,Space.Self);用于控制对象的旋 转。其中声明Angle属性的代码为:public float Angle;

(2)然后创建一个脚本,拖动到Canvas上,用于控制UGUI中的滑动条和按钮的行为

(3)在脚本中创建public GameObject obj; public Slider slider; public Button Button;

Obj中用于存放用于旋转的对象,slider中存放滑动条

(4)在Start方法中添加对应的监听功能 :slider.onValueChanged.AddListener(Onslider);

然后在Onslider方法中将滑动条中的Value值赋予给Rotate脚本中的Angle属性

public void OnSlider(float value)

{

obj.GetComponent<RotatePractice>().speed = slider.value;

}

(5)或者可以直接在Update()方法中添加 obj.GetComponent<RotatePractice>().speed = slider.value; 这样就可以不需要使用监听功能

其中可以使用枚举来选择物体对象的旋转轴

//定义旋转轴

private Vector3 direct;

public enum RotateDirect

{

X轴正方向,

X轴负方向,

Y轴正方向,

Y轴负方向,

Z轴正方向,

Z轴负方向

}

//声明枚举变量

public RotateDirect dir;

void Update()

{

switch(dir)

{

case RotateDirect.X轴正方向:

direct = Vector3.right; break;

case RotateDirect.X轴负方向:

direct = Vector3.left; break;

case RotateDirect.Y轴正方向:

direct = Vector3.up; break;

case RotateDirect.Y轴负方向:

direct = Vector3.down; break;

case RotateDirect.Z轴正方向:

direct= Vector3.forward; break;

case RotateDirect.Z轴负方向:

direct = Vector3.back; break;

}

//利用滑动条控制速度

this.transform.Rotate(direct, Angle * Time.deltaTime, Space.Self);

}


2. 利用按钮改变物体对象的颜色(使其生成随机颜色)

在控制UGUI的脚本中添加 public Button button;

在Start方法中添加监听功能 : button.onClick.AddListener(OnButton)

复制代码
void OnButton()
{
    Debug.Log("111");
    Ghost.GetComponentInChildren<SkinnedMeshRenderer>().sharedMaterial.SetColor("_Color",new Color(Random.value,Random.value,Random.value,1));
}

代码:

复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UI;

public class Rotate : MonoBehaviour
{
    //旋转的速度
    //public float speed;

    //利用滑动条控制速度
    public float Angle;

    private Vector3 direct;
    public enum RotateDirect
    {
        X轴正方向,
        X轴负方向,
        Y轴正方向,
        Y轴负方向,
        Z轴正方向,
        Z轴负方向
    }
    // Start is called before the first frame update

    //声明枚举变量
    public RotateDirect dir;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        switch(dir)
        {
            case RotateDirect.X轴正方向:
                direct = Vector3.right; break;
            case RotateDirect.X轴负方向:
                direct = Vector3.left; break;
            case RotateDirect.Y轴正方向:
                direct = Vector3.up; break;
            case RotateDirect.Y轴负方向:
                direct = Vector3.down; break;
            case RotateDirect.Z轴正方向:
                direct= Vector3.forward; break;
            case RotateDirect.Z轴负方向:
                direct = Vector3.back; break;
        }
        //自旋转  right代表X轴,up代表Y轴,forward代表Z轴
        //参数分别为旋转轴,旋转的角度,坐标系的选择
        //this.transform.Rotate(Vector3.up,Angle*10*Time.deltaTime,Space.Self);

        //利用滑动条控制速度
        this.transform.Rotate(direct, Angle * Time.deltaTime, Space.Self);

        //第二种,需要四个参数分别是X轴上旋转的角度、Y轴上旋转的角度、Z轴上旋转的角度,模型围绕旋转的坐标系
        //this.transform.Rotate(0, 30 * Time.deltaTime, 0, Space.Self);
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIInteractive : MonoBehaviour
{
    public GameObject Ghost;
    public Button button;
    public Toggle toggle;
    public Slider slider;  //滑动条

    void Start()
    {
        //当点击按钮时,触发按钮中onClick方法的监听功能
        button.onClick.AddListener(OnButton);
        toggle.onValueChanged.AddListener(OnToggle);
        slider.onValueChanged.AddListener(Onslider);
    }

    //按钮
    void OnButton()
    {
        Debug.Log("111");
        Ghost.GetComponentInChildren<SkinnedMeshRenderer>().sharedMaterial.SetColor("_Color",new Color(Random.value,Random.value,Random.value,1));
    }

    //开关,利用开关设置对象的显示和隐藏; 其中Toggle函数中需要添加一个布尔值的参数
    void OnToggle(bool ison)
    {
        Ghost.SetActive(ison);
    }

    void Onslider(float value)
    {
        //获取怪物身上的脚本组件中的Speed参数
        Ghost.GetComponent<Rotate>().Angle=slider.value;
    }
}

效果展示:

【太妃糖耶】Up主探索中,欢迎收看求三连!_哔哩哔哩_bilibili

相关推荐
天人合一peng8 小时前
unity 生成标记根据背景色标记变色
unity·游戏引擎
天人合一peng11 小时前
unity 生成标记根据背景色变色为明显的颜色
unity·游戏引擎
魔士于安12 小时前
Unity 超市总动员 超市收银台 超市货架 超市购物手推车 超市常见商品
游戏·unity·游戏引擎·贴图·模型
CandyU212 小时前
Unity —— 数据持久化
unity·游戏引擎
zh路西法12 小时前
【Unity实现Oneshot胶卷显形】游戏窗口化与Win32API的使用
游戏·unity·游戏引擎
迪捷软件13 小时前
显控系统虚拟仿真的工程化路径
游戏引擎·cocos2d
凡情17 小时前
android隐私合规检测
android·unity
小贺儿开发17 小时前
Unity3D 本地 Stable Diffusion 文生图效果演示
人工智能·unity·stable diffusion·文生图·ai绘画·本地化
Swift社区17 小时前
传统游戏引擎 vs 鸿蒙 System 架构
架构·游戏引擎·harmonyos
mxwin1 天前
Unity Shader 半透明物体为什么不能写入深度缓冲?
unity·游戏引擎·shader