How can i pass a method like a parameter Unity3d C#?
i'm trying to pass a function to another function as a parameter, how make this work?
This is an example of what i need to do:
CreateButton( new BuildModeController().BuildObject_BuildInstalledObject("Wall") );
How i must write the method?
void CreateButton( [What i need to write here?] )
{
. . . do something.
}
Have in consideration than BuildObject_BuildInstalledObject("Wall")
doesn't return anything (yes, is a void method), an itself requeire a parameter, this method actualy make something in the worldmap
this is the actual CreateButton method:
void CreateButton(Canvas panel, Vector2 newVector2Position, Vector2 newVector2size, Action<string> method, Sprite buttonSprite, string stringNameButton)
{
DicButtonSprite.TryGetValue ("TileBlancoTibia", out ButtonSprite);
//Create button & Text
Button button = (new GameObject ("Button", typeof(CanvasRenderer), typeof(Image), typeof(Button))).GetComponent<Button> ();
Text txt = (new GameObject ("Texto", typeof(CanvasRenderer), typeof(Text))).GetComponent<Text> ();
//Installing Hierarchys
button.transform.parent = panel.transform;
txt.transform.parent = button.transform;
//Posición y tamaño
button.transform.position = newVector2Position;
button.GetComponent<RectTransform> ().sizeDelta = newVector2size;
txt.transform.position = newVector2Position;
txt.GetComponent<RectTransform> ().sizeDelta = newVector2size;
//txt preparations
txt.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
txt.alignment = TextAnchor.MiddleCenter;
txt.text = stringNameButton;
txt.color = Color.black;
txt.fontSize = 12;
//Imagen
button.GetComponent<Image>().sprite = buttonSprite;
//Metodo al que llama
button.GetComponent<Button>().onClick.AddListener(method);
button.GetComponent<Text>().text = stringNameButton;
}
of course, this method doesn't work, and i'm looking to change the Action<string> method
part
i'm trying to archive this to add to a button, through an addListener, if you have that in consideration would be excelent.
Thanks in advance
like this?
function objc(){ return transform } - function parameter
function create(Transform t){}
I'm not in use or need of pass a transform, but i need to pass a function in order to add to the object through an addlistener, any ideas in how to archieve this? thanks in advance
Answer by ThePersister · Nov 29, 2016 at 12:58 PM
Here's an example of how you would pass a method along as a parameter.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public delegate void TestDelegate(); // This defines what type of method you're going to call.
public TestDelegate m_methodToCall; // This is the variable holding the method you're going to call.
void Start()
{
// Fill Delegate.
m_methodToCall = AwesomeExampleMethod; // Notice we don't use (); here. (that can be confusing)
// Call method, pass along delegate.
SimpleMethod( m_methodToCall );
}
// This method expects a TestDelegate variable, allowing us to pass a custom void method.
private void SimpleMethod(TestDelegate method)
{
Debug.Log( "I'm about to call a method" );
method();
}
// A random method that is compatible with TestDelegate (asks no parameters, returns void)
private void AwesomeExampleMethod()
{
Debug.Log( "Yay!" );
}
}
I hope that helps, if it did, please accept my answer. If you need any details, let me know!
Best of luck!
Cheers,
ThePersister
Extra details about the string parameter setup you want:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
public Button button;
public delegate void TestDelegate(string test); // This defines what type of method you're going to call.
public TestDelegate m_methodToCall; // This is the variable holding the method you're going to call.
void Start()
{
// Fill Delegate.
m_methodToCall = new Build$$anonymous$$odeController().BuildObject_BuildInstalledObject; // Notice we don't use (); here. (that can be confusing)
// Call method, pass along delegate.
Simple$$anonymous$$ethod( m_methodToCall );
}
// This method expects a TestDelegate variable, allowing us to pass a custom void method.
private void Simple$$anonymous$$ethod(TestDelegate method)
{
Debug.Log( "I'm about to call a method" );
button.onClick.AddListener( () => method("Wall") ); // Note how we use the "Wall" bit here.
}
// A random method that is compatible with TestDelegate (asks no parameters, returns void)
private void AwesomeExample$$anonymous$$ethod(string test)
{
Debug.Log( "Yay!" );
}
}
This is strange, it works, i mean i can use the method, the Debug.Log()'s emited when i press the button are telling me that it work, so thanks for helping me to learn delegates, now the thing is even when this method "Works" (i.e. pass the code through the stored method) it doesn't make the effect than i'm looking for -and suppose to do-, thanks in any case, i'm gonna gave you the answered question because, i think, this problem is for a new question, Thanks a lot!.
You're welcome, I'm glad I could help, best of luck with the future issues! :)
Answer by SeanKilleen · Nov 28, 2016 at 09:39 PM
You can use delegates in c# to pass functions as an argument to other functions.
https://msdn.microsoft.com/en-us/library/ms173171.aspx
public delegate int PerformCalculation(int x, int y);
void CreateButton(int x)
CreateButton(PerformCalculation(x,y));
Answer by AkarshJain · Jul 03, 2021 at 09:40 AM
for me the ans was
public Button button;
public void Func(UnityEngine.Events.UnityAction Func2){
// i wanted to use it to change button on click listener
button.onClick.Addlistner(Func2);
}
Your answer
Follow this Question
Related Questions
How do I call a method with variables in it? 1 Answer
Help with an error in scripting - extreme newbie 3 Answers
How can I change a function from another script? 1 Answer
[C#]How to save a list of items to use in another script and it's not a player prefs type of list? 0 Answers
How to automatically generate a new int with a specific name? 1 Answer