Unity如何从轴y = 260到10实例化预制件?
Unity how to instantiate a prefab from axis y = 260 to 10?
我如何在 2d 中从 pos y 110 向下实例化对象?
我还在搜索,但我找不到任何东西,所以我把我的问题放在这里,希望有人能帮助我。
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 () {
}
}
我想在 foreach
部分中放入实例化,因此如果我有 2 名员工,则必须创建 2 个按钮或预制件,我称之为 EmployeeTab
.
我是中级脚本的新手:/
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;
}
}
一旦你知道 how to instantiate,这就是小菜一碟! ;)
void Start ()
{
List<Employee> employees = new List<Employee>();
// Create a parent of your instantiated objects
GameObject parent = new GameObject("EmployeesParent");
// Position of the instantiated objects
Vector3 position = Vector3.up * 110.0f ;
// Distance between instantiated objects
float step = 10 ;
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)
{
costInfo.text = "Cost: " + CurrencyConverter.Instance.GetCurrencyIntoString(Employee.Cost, false, false, false);
EmployeeName.text = Employee.Name;
SkillInfo.text = Employee.Skill.ToString();
// Instantiate the objects from a given prefab
GameObject w = (GameObject) Instantiate(myPrefab, position, Quaternion.identity, parent.transform );
// Set the desired name
w.name = worker.name ;
// Don't forget to change the position of the next employee
position.y -= step ;
}
}
编辑:MonoBehaviour 不能像传统的 C# class 那样被实例化。由于您的 Employee class 似乎是一个简单的数据容器,您必须更改您的 Employee class 和我编写的 Start 函数:
using UnityEngine;
using System.Collections;
using System;
[System.Serializable]
public class Employee
{
public string name;
public int skill;
public int cost;
}
// YOUR OTHER SCRIPT
// Set the values from the inspector here
public Employee[] employees ;
void Start ()
{
// Create a parent of your instantiated objects
GameObject parent = new GameObject("EmployeesParent");
// Position of the instantiated objects
Vector3 position = Vector3.up * 110.0f ;
// Distance between instantiated objects
float step = 10 ;
for( int i = 0 ; i < employees.Length ; ++i )
{
costInfo.text = "Cost: " + CurrencyConverter.Instance.GetCurrencyIntoString(employees[i].cost, false, false, false);
EmployeeName.text = Employee.Name;
SkillInfo.text = Employee.Skill.ToString();
// Instantiate the objects from a given prefab
GameObject w = (GameObject) Instantiate(myEmployeePrefab, position, Quaternion.identity, parent.transform );
// Set the desired name
w.name = worker.name ;
// Add the component you want to the instantiated GameObject to show the information of your employee like a UnityEngine.UI.Text component or whatever you want
// Don't forget to change the position of the next employee
position.y -= step ;
}
}
我如何在 2d 中从 pos y 110 向下实例化对象?
我还在搜索,但我找不到任何东西,所以我把我的问题放在这里,希望有人能帮助我。
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 () {
}
}
我想在 foreach
部分中放入实例化,因此如果我有 2 名员工,则必须创建 2 个按钮或预制件,我称之为 EmployeeTab
.
我是中级脚本的新手:/
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;
}
}
一旦你知道 how to instantiate,这就是小菜一碟! ;)
void Start ()
{
List<Employee> employees = new List<Employee>();
// Create a parent of your instantiated objects
GameObject parent = new GameObject("EmployeesParent");
// Position of the instantiated objects
Vector3 position = Vector3.up * 110.0f ;
// Distance between instantiated objects
float step = 10 ;
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)
{
costInfo.text = "Cost: " + CurrencyConverter.Instance.GetCurrencyIntoString(Employee.Cost, false, false, false);
EmployeeName.text = Employee.Name;
SkillInfo.text = Employee.Skill.ToString();
// Instantiate the objects from a given prefab
GameObject w = (GameObject) Instantiate(myPrefab, position, Quaternion.identity, parent.transform );
// Set the desired name
w.name = worker.name ;
// Don't forget to change the position of the next employee
position.y -= step ;
}
}
编辑:MonoBehaviour 不能像传统的 C# class 那样被实例化。由于您的 Employee class 似乎是一个简单的数据容器,您必须更改您的 Employee class 和我编写的 Start 函数:
using UnityEngine;
using System.Collections;
using System;
[System.Serializable]
public class Employee
{
public string name;
public int skill;
public int cost;
}
// YOUR OTHER SCRIPT
// Set the values from the inspector here
public Employee[] employees ;
void Start ()
{
// Create a parent of your instantiated objects
GameObject parent = new GameObject("EmployeesParent");
// Position of the instantiated objects
Vector3 position = Vector3.up * 110.0f ;
// Distance between instantiated objects
float step = 10 ;
for( int i = 0 ; i < employees.Length ; ++i )
{
costInfo.text = "Cost: " + CurrencyConverter.Instance.GetCurrencyIntoString(employees[i].cost, false, false, false);
EmployeeName.text = Employee.Name;
SkillInfo.text = Employee.Skill.ToString();
// Instantiate the objects from a given prefab
GameObject w = (GameObject) Instantiate(myEmployeePrefab, position, Quaternion.identity, parent.transform );
// Set the desired name
w.name = worker.name ;
// Add the component you want to the instantiated GameObject to show the information of your employee like a UnityEngine.UI.Text component or whatever you want
// Don't forget to change the position of the next employee
position.y -= step ;
}
}