Reflection for class type in JsonUtility.FromJson<>

Hi Guys,

Pretty much in the question. I have this:

        public IEnumerator Execute(Action<ScenarioGroup> response)
        {
            yield return request.Send();

            var output = JsonUtility.FromJson<ScenarioGroup>(request.downloadHandler.text);
            response(output);
        }

I’d like to convert it to this:

public IEnumerator Execute(UnityEngine.Object someType, Action<UnityEngine.Object> response)
        {
            yield return request.Send();

            var output = JsonUtility.FromJson<someType.GetType()>(request.downloadHandler.text);
            response(output);
        }

Keep in mind I know the second one has no chance of working right now, but hopefully you get the gist of what I’m trying to achieve: Make it so it’s not a fixed class, but will return whatever class type you request in the parameters of the coroutine.

Any thoughts?

Found it myself :slight_smile: don’t worry

    public class Request<T>
    {
        public float Progress { get { return request.downloadProgress; } }
        public bool IsDone { get { return request.isDone; } }

        private UnityWebRequest request;

        public Request(MonoBehaviour monoBehaviour, string www, Action<T> asyncResponse) : this (www)
        {
            monoBehaviour.StartCoroutine(Execute(x => asyncResponse(x)));
        }
       
        public Request(string www)
        {
            request = UnityWebRequest.Get(www);
        }

        public IEnumerator Execute(Action<T> response)
        {
            yield return request.Send();

            var output = JsonUtility.FromJson<T>(request.downloadHandler.text);
            response(output);
        }
    }