Get the latest Education e-news
 
Old 08-09-2011, 01:19 PM   #1
JohnLeeroy
Junior Member

Activity Longevity
0/20 9/20
Today Posts
0/11 sssssss14
Default C# Gui

Hello,

I am designing a GUI system for my Unity3D project in C# and I want to handle button presses in the best way that I can.

Here's how I would do it in C++ ...

Code:
void StartNewGame()
{
     //SomeCode
}

void Main()
{
    ButtonClass NewGame;
    NewGame.FunctionPointerIfPressed = StartNewGame;
}
It is a bit trickier with C#. I was looking at delegates but I don't see how I could use it as a function pointer. Maybe you guys can give me an idea.


Bottom Line:
I have a container of a button class and I want each button to be able reference a function so I can call said function when button is pressed.
JohnLeeroy is offline   Reply With Quote
Old 08-10-2011, 03:47 AM   #2
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
2/11 sssss2255
Location: UK
Default

Should be pretty simple. In Unity, a Button should have a 'Clicked Event' function callback already in place for you to fill in.
yaustar is offline   Reply With Quote
Old 08-11-2011, 01:59 PM   #3
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
1/11 ssssss692
Location: Netherlands
Default

Maybe I'm oversimplifying here, but perhaps you mean something like this?

Code:
NewGame.Click += StartNewGame;
(And you'll probably have to overload StartNewGame with a variant that has an object and an EventArg as parameters, but I'm not sure. I'm not familiar with Unity.)
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is offline   Reply With Quote
Old 08-11-2011, 05:56 PM   #4
JohnLeeroy
Junior Member

Activity Longevity
0/20 9/20
Today Posts
0/11 sssssss14
Default

Quote:
Originally Posted by yaustar View Post
Should be pretty simple. In Unity, a Button should have a 'Clicked Event' function callback already in place for you to fill in.
The syntax is
Code:
if(GUI.Button(rectangle, "message"))
{
    //code here if pressed
}
If i am instantiating buttons during runtime, it is tricky by using that small block of code. I have considered giving each button a unique id and use a table to link each id to a function but i think there is a better way to do it.


ProtectorOne,
That's how buttons work in C# Forms Programming but I'm not sure how to set up an event system that supports that in Unity =/. Unity does support .NET 4.0.


Thank you for the replies.
JohnLeeroy is offline   Reply With Quote
Old 08-11-2011, 09:26 PM   #5
jwalters
Senior Member

Activity Longevity
0/20 15/20
Today Posts
0/11 ssssss142
Location: Apex NC, USA
Default

John, I'm not familiar with Unity, but I think what you're looking for are C# events and delegates. I haven't found a great article on them, but this one tries to explain:

http://www.codeproject.com/KB/cs/csevents01.aspx

Basically, you want to create your own custom events and event handlers (delegates/callbacks). If you need more info search on "C# delegates" or "C# event handling". Hope that helps.
__________________
Jeromie L. Walters
http://gamedeveloperjourney.blogspot.com
jwalters is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 08:52 AM.






UBM Tech