Я пытаюсь создать GameObject из префаба с «пользовательскими» аргументами аналогично тому, как конструкторы принимают аргументы. Unity, похоже, идет своим путем. Я нашел это:
public class Test : MonoBehaviour{
public Test Init(
){
...;
return this;
}
}
public class Instantiator : MonoBehaviour {
void Start(){
var newTest = Instantiate(prefab).Init();
}
}
... пытаюсь использовать приведенный выше код в своем коде:
Master_object.cs:
using UnityEngine;
using System.Collections;
public class Master_object : MonoBehaviour {
void Start(){
GameObject prefab = Resources.Load("Planet_object") as GameObject;
GameObject go = Instantiate
(prefab).Init(1, 1, 1);
}
}
Planet_object.cs:
using UnityEngine;
public class circle_script : MonoBehaviour {
[SerializeField] private LineRenderer circleRenderer;
public circle_script Init(int offset_x, int offset_y, float radius){
DrawCircle(100,offset_x,offset_y,radius);
return this;
}
void DrawCircle(int steps, int offset_x, int offset_y, float radius){
/* matching the radius of the collider with the radius of the planet.
gameObject.GetComponent().radius = radius ?*/
var collider = gameObject.GetComponent();
collider.radius = radius;
// drawing the circle
circleRenderer.positionCount = steps;
for(int currentStep = 0; currentStep
Подробнее здесь: https://stackoverflow.com/questions/792 ... b-in-unity
Как создать игровой объект из префаба в Unity ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение