Я пытаюсь создать 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#
Место общения программистов C#
-
Anonymous
1732865251
Anonymous
Я пытаюсь создать 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
Подробнее здесь: [url]https://stackoverflow.com/questions/79235402/how-to-create-a-gameobject-from-prefab-in-unity[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия