Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by QuantumFlin · Jan 11, 2015 at 06:23 PM · unityui

Creating clickable image on-the-fly

I am trying to implement an inventory system which allows me to dynamically (i.e. during runtime) create a UI-Image as visual representation for an item which can be moved around with the mouse and snaps in place in the inventory grid. SO basically, all I am doing needs to be accessible from c#-scripts.

However, I found it excessively difficult to use the built-in systems to detect a mouse click on the 2D item with e.g. IPointerDownHandler. I also tried using OnMouseDown(), but it doesn't seem to register. I also also tried this stuff of which I understand about half, so I'm just gonna post it:

     //Create new trigger
     EventTrigger evtrig=gameObject.AddComponent<EventTrigger>();

     //Create a new event to add to the trigger
     EventTrigger.TriggerEvent trigev=new EventTrigger.TriggerEvent();
     trigev.AddListener((eventData) => OnPointerDown((PointerEventData)eventData)); //link to onpointerdown function

     EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigev, eventID = EventTriggerType.PointerDown };

     if (evtrig.delegates==null) evtrig.delegates=new List<EventTrigger.Entry>();
     evtrig.delegates.Add(entry);

So I guess my question is this: Has anyone had success implementing a similar system? (The main point being that it has to be created through scripting) Maybe I am just plain wrong with my sample code here, too

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ben-rasooli · Dec 08, 2017 at 12:58 AM

I've made an extension method for it. Simply include this class somewhere in your project:

 using UnityEngine.EventSystems;
 
 public static class ExtensionMethods
 {
     public static void AddListener (this EventTrigger trigger, EventTriggerType eventType, System.Action<PointerEventData> listener)
     {
         EventTrigger.Entry entry = new EventTrigger.Entry();
         entry.eventID = eventType;
         entry.callback.AddListener(data => listener.Invoke((PointerEventData)data));
         trigger.triggers.Add(entry);
     }
 }

Then use it like this:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class UIController : MonoBehaviour
 {
     void Start ()
     {
         EventTrigger myEventTrigger = GetComponent<EventTrigger> (); //you need to have an EventTrigger component attached this gameObject
         myEventTrigger.AddListener (EventTriggerType.PointerClick, onClickListener);
     }
 
     void onClickListener (PointerEventData eventData)
     {
         //put your logic here...
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUIText does not display anything 1 Answer

Why is this error "Type or namespace UnityEngine.UI does not exist" in windows 1 Answer

Unity UI Background Accelerometer 0 Answers

Make touch buttons stop working after finger slides off. 1 Answer

Score UI update causes frames to skip IOS only 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges