iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1120921 根據輸入的名字在指定位置生成Prefab
by 黃國哲 2023-09-21 11:09:47, 回應(0), 人氣(290)
https://docs.unity3d.com/cn/2018.4/ScriptReference/Object.Instantiate.html

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

public class mapsearchitem : MonoBehaviour
{
    public TMP_InputField itemtext; //TMP的輸入框
    private string itemname;        //設備的名稱
    public GameObject 標記prefab;

    private float markx;
    private float marky;
    private float markz;

    //public GameObject 選單介面;
    //public GameObject 顯示介面;

    public void mapgetnamebtn()
    {
        itemname = itemtext.GetComponent<TMP_InputField>().text; //抓取輸入框的文字
        markx = GameObject.Find(itemname).GetComponent<Transform>().position.x;
        marky = GameObject.Find(itemname).GetComponent<Transform>().position.y;
        markz = GameObject.Find(itemname).GetComponent<Transform>().position.z;
        Instantiate(標記prefab, new Vector3(markx, marky, markz), new Quaternion(0, 0, 0, 0));
        //{ GameObject.Find(itemname).SetActive(true); }

        //顯示介面.SetActive(true);
        //選單介面.SetActive(false);
    }
回應