I am trying to create an game board by using Unity and NGUI and everything is working ok now. I am using the DragNDrop script on my dragable object and on my fields on the board I am using UIDragDropContainer. But, is there a way I can tell my dragable object which field the object is over or attaching it self to?
I have scouted the net, but have not found an solution.
Any help is appreciated :-)
Use the UICamera.hoveredObject to get the last gameObject (with a collider) that the mouse or touch was last over.
In one of my projects I came across a same issue. What I did was inherit the NGUI's UIDragDropItem script and created a delegate to tell me about the object. I have also added the source:
public class Custom_DragDropItem : UIDragDropItem {
public delegate void DragDropDelegate(GameObject TargetObject);
public DragDropDelegate onDragDrop;
}
All you have to do is use this script instead of UIDragDropItem on your draggable items in the inspector and register to the delegate onDragDrop and it will pass you the target object on which the item is dropped.
Related
I'm sure that I am tangled in my own code so it would be easy I bet to help me out of it I would really appreciate someone throwing me a line.
Can you attached a script to a button and then call a method in that script using that button's onClick in the unity inspector?
just so you know, you create a specific 'button' in unity, not a GameObject with a sprite, so that it has a built in unity function where you write a very short piece of code:
public void FunctionName()
{
Do(what_youWant.here)
}
and put the script on the actual button, then have a look at the button's components list, and you'll see a place for functions; there you select (from the dropdown) your function from the code.
I have an event manager in Unity which can handle events sent from scene components. The classes are all a bit abstracted from the standard Unity components. I am successfully able to fire an event from a button, and have that button's parent listen to the event (the onEventReceived you see below).
Using the Unity Debugger extension for VSCode, I can log the sender. If I type sender.gameObject and it returns a correct reference to the GameObject the sender is attached to. However, I cannot reference sender.gameObject within the OnEventReceived function itself - because it is of type ISceneComponent I suppose. How do I get a reference to the game object of the clicked sender?
I have run into your issue myself before.
The fix is quite simple.
Assuming that you created the interface ISceneComponent, add the gameObject field to your Interface
public interface ISceneComponent
{
GameObject gameObject { get; set; }
// ...
}
If the object implementing ISceneComponent also is a monobehaviour, gameObject will automatically be filled and you can reference it easily.
Um.. First thing's first. I must let you know that I don't have core knowledge in C# scripting at all.
Alrighty, so here's my case. :)
Let's say I have a Script named AIEnemy with this snippet:
public delegate void CallbackOnEnemyAttack(int attackDamage);
public static event CallbackOnEnemyAttack OnEnemyAttack;
And I have a GameManager Script that sits on a one-and-only empty GameObject.
The case is... the AIEnemy Script is attached to lots of other GameObjects in the Scene. And I'm left wondering if the delegate AND the event that I declared in the script are of single instance or will be different per AIEnemy component attached to every GameObject.
I'm kind of a neat freak when it comes to game architecture even though I'm a noob. I'm super interested in delegates/events in Unity to design the architecture of my scripts better since I really don't like how Unity doesn't expose public instances of Interfaces in the Inspector that I have to do a "hacky" way to get around it by casting/tightly coupling my scripts together.
The idea of making an event static in Unity is so that you can easily subscribe to it without needing an instance of it. This should be used in a script with one instance and this means it must be attached to one GameObject only.
That delegate and event code should be put in a script called EventManager or somthing similar. This EventManager script is a script that is attached to one empty GameObject. Your enemy and other scripts can subscribe to this EvenetManager when they are spawned and also receive notification when there is an event.
What happens when you attach this multiple GameObjects and now it has
multiple instances?
If you trigger and event, each script you attached to a GameObject will send their own event. If this is attached to 500 enemies and you trigger event, all the 500 enemies will send an event to the subscriber.
In most cases, it is better to make these variables private and expose a function that can be used to subscribe and trigger events on them.
This has been done already. Here is a complete example of EventSystem from Unity that uses Unity's UnityEvent which is slow. Here is the ported version I made that uses delegate and should be used if possible.
Subscribing to an event:
EventManager.StartListening("Spawn", SomeOtherFunction);
Un-Subscribing to an event:
EventManager.StopListening("Spawn", SomeOtherFunction);
Triggering an event:
EventManager.TriggerEvent("Spawn");
I want to select a gameobject at run time in unity and move it using an UI button.
for example if there are three cubes in the scene i want to select one cube and one that I clicked will move once I click the UI button .
I have no idea how to pass the value of the selected object to the button click event.If some one can show it using a sample code it would be great.
You can set a tag on the object and then use the GameObject.FindGameObjectsWithTag() function to get that object.
Here is the documentation for the function: https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
Use this:
var obj = Selection.activeObject;
When working with objects that are primarily in a scene, it is strongly recommended to use below code instead:
var transform = Selection.activeTransform;
Refer to official doc here.
Well,I am working on my graduation project with unity 3d pro v4.5.0 ..I was using the normal method OnGUI() in my scripts but now i want to make a better interface for my project and i didn't anderstand how can i replace for example these lines in my "Database" Script:
if (GUILayout.Button ("Add to database"))
{
// Insert the data
InsertRow (FirstName,LastName);
// And update the readout of the database
databaseData = ReadFullTable ();
}
With just calling a Button1_click for example from my iGUI Class specially that every thing related to interface and buttons exists in OnGUI() method so how can i change all of that in my "Database" Script..i did anderstand how to make a button in iGUI that LOAD a scene or set an other function in the same iGUI Class but i just want to make buttons and iGUI field texts in iGUI MonoBehaviour Class and call them later whenever i need in my pricipal script "Database" if it is possible.Please am so in need for help. Thank you in advance.
You need do this:
create a public method in any script.
add the script to a GameObject.
Click the button and add the object with the script in the method
OnClick located in the inspector.
Choose your script and the public method where said No function.
Boala!!, when you click, your method run.