I’m having trouble with prefab order in a parental container. Being given a list having ids of the 3d models that we have posted them into database, now, I’m using the id to load them into our game.
But the problem I’m having is that some of them will have a different loading and cloning time, so the order of these prefabs in the container is wrong.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using Unity.VisualScripting;
public class LoadModelManager:MonoBehaviour {
static public IEnumerator LoadModel( int? id, GameObject self, GameObject parent = null, bool active = false, int indexOrder=0)
{
if (id == null)
{
id = 0; // skin default
}
string url = $"{GlobalVariable.server_url}/3ds/download/{id}"; // Replace with your server and image id
using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(url))
{
yield return webRequest.SendWebRequest();
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.Log(webRequest.error);
}
else
{
AssetBundle remoteAssetBundle = DownloadHandlerAssetBundle.GetContent(webRequest);
if (remoteAssetBundle == null){
yield break;
}
Debug.Log(remoteAssetBundle.LoadAsset(remoteAssetBundle.GetAllAssetNames()[0]));
var model = Instantiate(remoteAssetBundle.LoadAsset(remoteAssetBundle.GetAllAssetNames()[0])) as GameObject;
model.transform.position = self.transform.position;
model.transform.rotation = self.transform.rotation;
model.transform.localScale = self.transform.localScale;
if (parent !=null){
model.transform.SetParent(parent.transform,false);
}
if (indexOrder!=0){
model.transform.SetSiblingIndex(indexOrder);
}
if (active == true){
model.SetActive(true);
}
else {
model.SetActive(false);
}
remoteAssetBundle.Unload(false);
}
}
}
}
I have tried to use SetSiblingIndex but seems like it still gives wrong order.
If anyone has solved this problem, please let me know how to solve this. I will be really appreciated.
Thank you. Have a nice day!