Hi,
I have been working on adding the addressable system to my project, and one of the things I want to do is press a button in the inspector on my editor script and load a list of objects using their addresses. I have been trying to use several methods that work during play, but not in the editor. Is there something that needs to be done to make it work in the editor?
Here is what I have so far, and this doesn’t work at all (OnBGDLoad() doesn’t run once). Forgive me if I have made a stupid mistake, I am still relatively new to C#:
[CustomEditor(typeof(LoadTexture))]
public class LoadTextureEditor : Editor
{
Camera cam;
public void OnEnable()
{
EditorApplication.update += Update;
}
public void OnDisable()
{
EditorApplication.update -= Update;
}
public override void OnInspectorGUI()
{
var lt = target as LoadTexture;
base.DrawDefaultInspector();
EditorGUILayout.Space();
if (GUILayout.Button("Save addresses and delete textures"))
{
lt.ConvertToAddressables();
}
if (GUILayout.Button("Reload textures"))
{
lt.reload = true;
}
}
void Update()
{
var lt = target as LoadTexture;
if (lt.reload)
{
Debug.LogError("Reload Executed");
lt.reload = false;
lt.allCams = Resources.FindObjectsOfTypeAll<Camera>();
foreach (Camera c in lt.allCams)
{
if (c.name != "SettingsCam" && c.name != "SceneCamera" && c.name != "Preview Camera")
{
if (c.transform.parent.Find("Background") != null)
{
cam = c;
Addressables.LoadAssetAsync<Sprite>(c.GetComponent<LoadTexture>().backgroundTextureAddress).Completed += OnBGDLoad;
}
}
}
}
}
private void OnBGDLoad(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<Sprite> op)
{
cam.transform.parent.Find("Background").GetComponent<SpriteRenderer>().sprite = op.Result;
Debug.LogError(cam.name);
}
}
P.S. The reason I am using the Update method is because I was having trouble getting foreach loops to run in editor, so I thought if I use update, I can force it to run as long as I have that inspector open (based on my understanding of how EditorApplication.update works)