iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1100318 從3D物件讀取頁面的架構
by 黃國哲 2021-03-18 23:34:50, Reply(0), Views(779)

1.點選3D模型會在畫面最上方顯示該模型名稱與確認按鈕

2.點選3D模型時傳送訊息
   (1)該建築的名稱
   (2)對應的scene名稱

將最上面的文字框命名為 buildingname,而其內容文字為buildingname.text
點擊到該模型會傳送該名稱到buildingname.text去顯示
同時傳送對應的模型名稱scene到確認button裡






實際Script如下:

先放置一個3D模型,在該模型上面掛上腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class 建築3D : MonoBehaviour
{
    // Start is called before the first frame update
    public Text buildingnametext;
    private string buildingname;

    void Start()
    {  }

    // Update is called once per frame
    void Update()
    {  }

    void OnMouseDown()
    {
        buildingname = this.gameObject.name;
        buildingnametext.text = buildingname;
    }
}

放置一個確認按鈕,在該按鈕上放置腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class 確認Btn : MonoBehaviour
{
    // Start is called before the first frame update

    //public GameObject player;

    public Text buildingnametext;

    public void 確認BTN()
    {   
         SceneManager.LoadScene(buildingnametext.text);
    }
}

在一般的2D物件上放置腳本,透過Event Trigger去使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class 物件2DTrigger : MonoBehaviour
{
    // Start is called before the first frame update
    public Text buildingnametext;
    private string buildingname;

    void Start()
    {   }
    // Update is called once per frame
    void Update()
    {   }
    public void BTNbuilding()
    {
        if (Input.GetMouseButton(0))
        buildingname = this.gameObject.name;
        buildingnametext.text = buildingname;
    }
}




便可以完成點擊物件並按下確認,可以傳送到該場景




Reply