オブジェクトをインスタンス化するにはどうすればいいですか?2dでは、pos 110から下方にオブジェクトをインスタンス化できますか?Unity軸からのプレハブのインスタンス化方法y = 260から10?
私はまだ検索していますが、何も見つからないので、私はここに私の質問を入れて、誰かが私を助けることを願っています。
EmployeeList.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class EmployeeList : MonoBehaviour {
public GameObject EmployeeTab;
// Use this for initialization
void Start()
{
List<Employee> employees = new List<Employee>();
// Create a parent of your instantiated objects
GameObject parent = GameObject.Find("Recruit_Employee");
// Position of the instantiated objects
Vector3 position = Vector3.up * 110f;
// Distance between instantiated objects
float step = 50;
employees.Add(new Employee("David", 5, 5000));
employees.Add(new Employee("Jason", 10, 10000));
employees.Add(new Employee("Donald", 3, 3000));
foreach (Employee worker in employees)
{
// Instantiate the objects from a given prefab
GameObject w = (GameObject)Instantiate(EmployeeTab, position, Quaternion.identity, parent.transform);
w.SetActive(true);
// Set the desired name
w.name = worker.name;
// Don't forget to change the position of the next employee
position.y -= step;
Debug.Log("Name: " + worker.name + "Skill: " + worker.skill + "Cost: " + worker.cost);
}
}
// Update is called once per frame
void Update() {
}
}
と私は2人の従業員を持っている場合、私は
EmployeeTab
を呼び出した後、2つのボタンまたはプレハブを作成する必要がありますので、
foreach
セクションでインスタンス化に載せていきたいと思います。
中間スクリプトに新しいイム:/
Employee.cs:
using UnityEngine;
using System.Collections;
using System;
public class Employee : MonoBehaviour
{
public string name;
public int skill;
public int cost;
public Employee(string newName, int newSkill, int newCost)
{
name = newName;
skill = newSkill;
cost = newCost;
}
}
をあなたがこれまでにそれをインスタンス化するために何をしようとしましたか?また、オブジェクトをインスタンス化しようとしていますか?テキストフィールド? –