void Start() { Vector3 objectSize = ArUtils.GetObjectSizeByMeshFilter(FollowObject); followObjectOffset = new Vector3(0, objectSize.y / 2 + .1f, 0); transform.position = FollowObject.transform.position + followObjectOffset; }
// Start is called before the first frame update void Start() { msgText = GameObject.Find("Text").GetComponent <Text>(); Vector3 objectSize = ArUtils.GetObjectSizeByMeshFilter(FollowObject); followObjectOffset = new Vector3(0, objectSize.y / 2 + .1f, 0); transform.position = FollowObject.transform.position + followObjectOffset; }
// Start is called before the first frame update void Start() { UnityEngine.Object messageBubblesModel = ArUtils.LoadModel("Model/MessageBubbles"); var messageBubbles = Instantiate(messageBubblesModel, Vector3.zero, Quaternion.Euler(Vector3.zero)) as GameObject; MessageBubbles = messageBubbles; MessageBubbles.transform.parent = GameObject.Find("Canvas").transform; //MessageBubbles.GetComponent<RectTransform>() //.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, ARUtils.GetObjectSize(gameObject).x*100); ; print(" ARUtils.GetObjectSize(gameObject).x:" + ArUtils.GetObjectSizeByMeshFilter(gameObject).x); words = SplitSentence(taskDesc); MessageBubbles.SetActive(false); Button[] buttons = MessageBubbles.GetComponentsInChildren <Button>(); previousSentence = buttons[0]; nextSentence = buttons[1]; showText = MessageBubbles.GetComponentInChildren <Text>(); previousSentence.onClick.AddListener(() => { if (index <= 0) { return; } index--; showText.text = words[index]; }); nextSentence.onClick.AddListener(() => { if (index >= words.Length - 1) { return; } index++; showText.text = words[index]; }); }
// Update is called once per frame void Update() { if (transform.position.z < 0 && GetComponent <MeshRenderer>().isVisible == false) { MessageBubbles.SetActive(false); } Ray ray = cam.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo)) { // 如果点击物体 if (Input.GetMouseButtonDown(0) && hitInfo.transform.name == transform.name) { MessageBubbles.SetActive(!MessageBubbles.activeSelf); showText.text = words[index]; } } if (MessageBubbles.activeSelf) { float distance = Vector3.Distance(transform.position, cam.transform.position); print(distance); // 消息框随模型移动缩放 float scale = 20 / distance; Vector3 screenPoint = cam.WorldToScreenPoint(transform.position + new Vector3(0, ArUtils.GetObjectSizeByMeshFilter(gameObject).y, 0)); MessageBubbles.transform.position = screenPoint; MessageBubbles.transform.localScale = Vector3.one * scale; } }