- Home /
How to use conditionals with delegates?
I have one script with the following delegate below, which is fired when the URL of the website displayed in my application has been changed either by user input or due to a page redirect:
public delegate void URLChangedDelegate (UWKWebView view, string url);
In a second script, what I would like to do is set a conditional so that I can check "if" the changed URL is a specific value and if so then load a new scene. I'm not quite sure how to do this as I have little experience with delegates. I have watched the official tutorials but they do not seem to cover this specific case. I would appreciate some advice please.
Answer by Owen-Reynolds · Dec 20, 2014 at 06:31 PM
That looks like a built-in for the uWEBkit AddOn? If so, might help to put that in the title, and I see they have a message board.
Delegates are just functions. So, you can write:
void checkForCats(UWKWebView view, string url) {
if(url=="catsthatlooklikehitler.com") // real page!! super cute!!
Debug.Log("this guy's in for a real treat.);
}
Then you hook it up as a callback however it says to. Like maybe you have to write URLchangeAction=checkForCats;
(but I just made up URLchangeAction.) Then it's kind of like OnCollisionEnter. Their system just "knows" when to run it.
Thanks Owen, and yes my specific example was from UWeb$$anonymous$$it since that is what I'm working with at the moment. $$anonymous$$y question though was a general use case of delegates, hence why I am asking here. I think I have a better understanding of this now. So if the delegate is defined in one script and I want to call it from another script, I simply use getcomponent and then call the delegate similar to how a function is called?
Edit: Never $$anonymous$$d I've worked it out. $$anonymous$$y assumption was correct :)
The original word for delegate is "function pointer." The idea is you have have a variable that holds a function. Simple idea, been in use for 30+ years. But C# renamed them "delegates" just to make them more confusing (function pointer is what it is. delegate is what it does, sort of.)
Your answer
Follow this Question
Related Questions
|| conditional statement not working 1 Answer
Else and If 2 Answers
#if UNITY_WEBPLAYER been removed? 2 Answers
How to make a callback function 3 Answers
Code in one function call being executed on multiple frames 0 Answers