Сначала мне кажется очень странным, что GenerateEmployees
должен быть инстанцированным методом. Вы создаете новый Employee
, чтобы потом генерировать больше. Скорее, это должен быть метод static
, я думаю!
[Serializable]
public class Employee
{
public string Name;
public string Job;
public string ImageUrl;
// RandomName will also have to be static
public static List<Employee> GenrateEmployees(int noOfEmployees)
{
// Lists in c# grow dynamically bigger.
// It is more efficient to already set the final size
var emp = new List<Employee>(i);
for (int i = 0; i < noOfEmployees; i++)
{
emp.Add(new Employee()
{
Name = RandomName(),
Job = RandomName(),
imageUrl = "https://url/image/placeimg.jpg"
}
);
}
return emp;
}
}
Тогда к префабу сотрудника должен быть просто присоединен определенный компонент (скрипт), например,
public class EmployeeController : MonoBehaviour
{
// These you reference via the Inspector in the prefab
[SerializeField] private Image image;
[SerializeField] private TextMeshProUGUI nameField;
[SerializeField] private TextMeshProUGUI designationField;
// And finally have a method you can call directly
// Using SendMessage is very inefficient and unsecure
public void BindData(Employee data)
{
nameField.text = data.Name;
designationField.text = data.Job;
// Start downloading the image
StartCoroutine(DownloadImage(data.ImageUrl));
}
private IEnumerator DownloadImage(string url)
{
using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(url))
{
yield return www.SendWebRequest();
if (www.isNetworkError || uwr.isHttpError)
{
Debug.Log(www.error);
}
else
{
// Get downloaded texture
var texture = DownloadHandlerTexture.GetContent(www);
// Create a sprite from the texture
var sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), Vector2.one * 0.5f);
// Assign it to the image
image.sprite = sprite;
}
}
}
}
И, наконец, вы просто использовали бы метод static
для создания данных Employee
, а затем использовали бы GetComponent
для получения ссылки на компонент EmployeeController
в созданном префабе. Еще лучше не использовать Resources
вообще! Вместо этого поместите сборный файл в обычную папку и сразу перетащите его в готовое поле CrrateEmployeeInfoTiles
public class CreateEmployeeInfoTiles : MonoBehaviour
{
// NOTE: Do NOT use Resources!
// rather place your prefab in any other folder and then simply drag it in here
// via the Inspector!
// By using directly the correct type instead of GameObject you
// a) do not need the GetComponent later since Instantiate already returns the dsme type
// b) have more security since now you only can drag&drop a GameObject here
// that actually has the required component attached
[SerializeField] private EmployeeController employeePrefab;
// Start is called before the first frame update
private void Start()
{
var employeeData = Employee.GenrateEmployees(6).ToArray();
var j = 10;
for (int i = 0; i < employeeData.Length; i++)
{
// Since employeePrefab is of type EmployeeController
// Instantiate already returns the component reference
var employee = Instantiate(employeePrefab, new Vector3((i * 10), (j * 20), 115), Quaternion.identity);
employee.BindData(emp[i]);
j =+ 30;
}
}
}
Напечатано на смартфоне, но я надеюсь, что идея проясняется