• 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
Question by lsheslop · Sep 20, 2021 at 10:21 PM · uicolliderbuttonui image

How to highlight a selected inventory slot.

Hey, Im new to learning C#. I have been following a tutorial on YouTube about an inventory system. However, I want to highlight a selected inventory slot without highlighting the remaining slots. I can't seem to figure out how to do this. Sorry if this is a beginner problem.

 public MouseItem mouseItem = new MouseItem();
 public GameObject inventoryPrefab;
 public InventoryObject inventory;
 public int X_START;
 public int Y_START;
 public int X_SPACE_BETWEEN_ITEMS;
 public int NUMBER_OF_COLUMN;
 public int Y_SPACE_BETWEEN_ITEMS;
 public bool hover = false;
  

 Dictionary<GameObject, InventorySlot> itemsDisplayed = new Dictionary<GameObject, InventorySlot>();
 // Start is called before the first frame update
 void Start()
 {
     CreateSlots();
 }

 // Update is called once per frame
 void Update()
 {
     UpdateSlots();
 }

  public void UpdateSlots()
 {
     foreach (KeyValuePair<GameObject, InventorySlot> _slot in itemsDisplayed)
     {
         if (_slot.Value.ID >= 0)
         {
             _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[_slot.Value.item.Id].uiDisplay;
             _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().color = new Color(1, 1, 1, 1);
             _slot.Key.GetComponentInChildren<TextMeshProUGUI>().text = _slot.Value.amount == 1 ? "" : _slot.Value.amount.ToString("n0");
         }
         else
         {
             _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().sprite = null;
             _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().color = new Color(1, 1, 1, 0);
             _slot.Key.GetComponentInChildren<TextMeshProUGUI>().text = "";
         }
     }
 }

 public void CreateSlots()
 {
     itemsDisplayed = new Dictionary<GameObject, InventorySlot>();
     for (int i = 0; i < inventory.Container.Items.Length; i++)
     {
         var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
         obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
 
         
         AddEvent(obj, EventTriggerType.PointerEnter, delegate { OnEnter(obj); });
         AddEvent(obj, EventTriggerType.PointerExit, delegate { OnExit(obj); } );
         AddEvent(obj, EventTriggerType.BeginDrag, delegate { OnDragStart(obj); } );
         AddEvent(obj, EventTriggerType.EndDrag, delegate { OnDragEnd(obj); } );
         AddEvent(obj, EventTriggerType.Drag, delegate { OnDrag(obj); } );


         itemsDisplayed.Add(obj, inventory.Container.Items[i]);
     }
     
 }

 private void AddEvent(GameObject obj, EventTriggerType type, UnityAction<BaseEventData> action)
 {
     EventTrigger trigger = obj.GetComponent<EventTrigger>();
     var eventTrigger = new EventTrigger.Entry();
     eventTrigger.eventID = type;
     eventTrigger.callback.AddListener(action);
     trigger.triggers.Add(eventTrigger);
 }

 private void OnTriggerEnter(Collider other)
 {
     GetComponent<Image>().color = new Color(0.2924528f, 1, 0.2924528f, 1);
                   
 }

 public void OnEnter(GameObject obj)
 {
     mouseItem.hoverObj = obj;
     if(itemsDisplayed.ContainsKey(obj))
     {
         mouseItem.hoverItem = itemsDisplayed[obj];
         hover = true;
     }

 }

 public void OnExit(GameObject obj)
 {
     mouseItem.hoverObj = null;
     mouseItem.hoverItem = null;
     hover = false;      
 }

 public void OnDragStart(GameObject obj)
 {
     var mouseObject = new GameObject();
     var rt = mouseObject.AddComponent<RectTransform>();
     rt.sizeDelta = new Vector2(100, 100);
     mouseObject.transform.SetParent(transform.parent);
     if(itemsDisplayed[obj].ID >= 0)
     {
         var img = mouseObject.AddComponent<Image>();
         img.sprite = inventory.database.GetItem[itemsDisplayed[obj].ID].uiDisplay;
         img.raycastTarget = false;
         hover = true;
     }
     mouseItem.obj = mouseObject;
     mouseItem.item = itemsDisplayed[obj];
 }

 public void OnDragEnd(GameObject obj)
 {
     if (mouseItem.hoverObj)
     {
         inventory.MoveItem(itemsDisplayed[obj], itemsDisplayed[mouseItem.hoverObj]);
     }
     else
     {
         inventory.RemoveItem(itemsDisplayed[obj].item);
     }
     Destroy(mouseItem.obj);
     mouseItem.item = null;
     hover = false;

 }

 public void OnDrag(GameObject obj)
 {
     if(mouseItem.obj != null)
     {
         hover = true;
         mouseItem.obj.GetComponent<RectTransform>().position = Input.mousePosition;
     }
         
 }
 
 public Vector3 GetPosition(int i)
 {
     return new Vector3(X_START + (X_SPACE_BETWEEN_ITEMS *(i % NUMBER_OF_COLUMN)), Y_START + (-Y_SPACE_BETWEEN_ITEMS * (i/NUMBER_OF_COLUMN)), 0f);
 }
 

}

public class MouseItem { public GameObject obj; public InventorySlot item; public InventorySlot hoverItem; public GameObject hoverObj; }

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

259 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make it so only a certain section of a button will work on click 1 Answer

How to Change Menu Button Rollover States with GameObjects 1 Answer

Displaced UI RectTransform collider in some resolutions issue. 1 Answer

UI image with button component not tracking mouse correctly 1 Answer

UI image with collider doesn't work OnMouseDown 1 Answer


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