D
D
dark_spectator2019-01-26 20:25:14
C++ / C#
dark_spectator, 2019-01-26 20:25:14

callback unity?

There is a coroutine in C#, I want to pass to it a method that should process its response as an argument, how can I do this? The coroutine is called from another file.
The result of executing the Callback method must be stored in a variable.

namespace Assets.Scripts.Core
{

    public delegate string Callback(string data);
    
    // ReSharper disable once UnusedMember.Global
    [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
    public class NetworkService : MonoBehaviour
    {
        [Header("Statements")]
        public static bool authorize = false;
        
        [Header("API data")]
        [SerializeField] private string userToken;       
        [SerializeField] private const string appKey = "";

        

        
        private void Start()
        {
            if (PlayerPrefs.HasKey("UserToken"))
            {
                authorize = true;
                userToken = PlayerPrefs.GetString("UserToken");
            }
        }

        public IEnumerator SetStatusOnline(Func<string, string> Callback) 
        {
            var uri = "https://vk.com";
            var query = new WWW(uri);
            yield return query;
            Callback(query.text);

        }
        
    }


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-01-26
@dark_spectator

What is wrong with your current version?
Well, apart from the fact that you didn't show how you are trying to call a coroutine)
, there is a public Enemulator.
You will somehow get a link to the class with it))
call. pass your function ... (well, or you can simply pass the method there

public IEnumerator SetStatusOnline(Action onDone)
{
....
///проверить только что Action не null
onDone?.Invoke();
....
}

what exactly does not suit that??

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question