物品展示部分
6-文字简介升级版
问题出现
上一篇博客中我们为每一个展示的物体都添加了3DText的子物体,并修改其中的Text变量为各个物体的简介,但是在我们测试过程中发现,这并不是我们想要的结果,因为当展品出现时,AR文字文字就会立即出现,而非当我们点击展示按钮时才出现该物体的简介
改进方法
于是我们决定把该方法整合进display按钮中,使其与物体旋转同时触发。
我们打开我们已经完成的item_rotate脚本,观察其逻辑
if (button_controller.GetComponent<click_control>().isRotate)
{
this.transform.Rotate(new Vector3(0, 0, 1 * Time.deltaTime * speed));
}
当click_control脚本中的变量isRotate为true时,将会出发物体的旋转效果。于是我们就将Text的显示与消失的效果实现放在这里。
首先创建一个TextMesh的对象
public GameObject text;
在Unity的界面中为其赋值
接着我们回到脚本中
将该物体的失活与激活放入Update方法中
if (button_controller.GetComponent<click_control>().isRotate)
{
this.transform.Rotate(new Vector3(0, 0, 1 * Time.deltaTime * speed));
text.SetActive(true);
}
else
{
text.SetActive(false);
}
这样就完成了。该脚本的整体代码为
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class item_rotate : MonoBehaviour
{
public float speed = 60f;
public GameObject button_controller;
public GameObject text;
private Vector3 OldRotation;
// Start is called before the first frame update
void Start()
{
button_controller = GameObject.Find("Button_click_manager").gameObject;
OldRotation = new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z);
}
// Update is called once per frame
void Update()
{
if (button_controller.GetComponent<click_control>().isRotate)
{
this.transform.Rotate(new Vector3(0, 0, 1 * Time.deltaTime * speed));
text.SetActive(true);
}
else
{
text.SetActive(false);
}
}
}
当然还有很重要的一点不要忘记,就是将Unity界面中的文字物体失活,否则该脚本无法正常运行