How can I make toggles to work separetrly in prefab?

Started by
1 comment, last by neilae 9 months ago

Hi, maybe someone would know, how to help me. I'm a begginer making 2D game in Unity and I wanted to create a toggle inside the prefab from asset that I installed. I have a map, and on it there are markers. When clicked, they show a specific information about this place, like name or description. All of this works fine. However, I also wanted to add a toggle to check if the user has visited this place or not.

I tried to add this toggle inside the script for this prefab (there are also created other information). However, each time something doesn't work as I wanted and I've tried many different methods. Mostly, all toggles for all markers were showing the same and I want them to work separetly. For example if for one place variable B is lower then 5, then it unables to click on the toggle. If it is higher, then user can tick it and the toggle is becoming unable again or it change to an image. I'm adding the whole scrip for a better view

[AddComponentMenu("Infinity Code/Online Maps/Demos/UIBubblePopup")]
    public class UIBubblePopup : MonoBehaviour
    {
       
        public Canvas canvas;

        public GameObject bubble;
        public Text title;
        public Text NotVisited;
        public Text Visit;
        public Text wojewodztwo;
        public Text point;
        public Text describtion;
        public Texture2D markerTip;
        public Toggle m_Toggle;
        public int b;
        public CData[] datas;
        public Text m_Text;        
        public Color disabledColor;  
        public Color enabledColor;
        public RawImage acceptedPhoto;

        /// <summary>
        /// Reference to active marker
        /// </summary>
        private OnlineMapsMarker targetMarker;



        /// <summary>
        /// This method is called by clicking on the map
        /// </summary>
        private void OnMapClick()
        {
            // Remove active marker reference
            targetMarker = null;

            // Hide the popup
            bubble.SetActive(false);
        }

        /// <summary>
        /// This method is called by clicking on the marker
        /// </summary>
        /// <param name="marker">The marker on which clicked</param>
        private void OnMarkerClick(OnlineMapsMarkerBase marker)
        {
            // Set active marker reference
            targetMarker = marker as OnlineMapsMarker;

            // Get a result item from instance of the marker
            CData data = marker["data"] as CData;
            if (data == null) return;

            // Show the popup
            bubble.SetActive(true);

            // Set title and address
            title.text = data.title;
            describtion.text = data.describtion;
            
           
            ColorBlock cb = m_Toggle.colors;


            if (data.b<5)
            {
                data.m_Toggle.gameObject.SetActive(true);
                data.m_Toggle.isOn= false;
                data.m_Toggle.enabled =false;
                m_Text.text=data.NotVisited;
               
                m_Text.color = data.disabledColor;
                cb.normalColor = data.disabledColor;
                cb.highlightedColor = data.disabledColor;
                cb.pressedColor = data.disabledColor;
                m_Toggle.colors = cb;
                data.acceptedPhoto.gameObject.SetActive(false);

               

            }
            else
            {
                
                data.m_Toggle.enabled = true;
                ToggleValueChanged(data.m_Toggle);
                cb.normalColor = data.enabledColor;
                cb.highlightedColor = data.enabledColor;
                cb.pressedColor = data.enabledColor;
                m_Toggle.colors = cb;

                
                data.m_Toggle.onValueChanged.AddListener(delegate { ChangeType(data.m_Toggle); });
                

            }

            
            void ToggleValueChanged(Toggle change)
            {
                m_Text.text = data.Visit;
                m_Text.color =data. enabledColor;
            }

            void ChangeType(Toggle m_Toggle)
            {
                data.m_Toggle.gameObject.SetActive(false);
                data.acceptedPhoto.gameObject.SetActive(true);
            }



            // Initial update position
            UpdateBubblePosition();
        }





        /// <summary>
        /// Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.
        /// </summary>
        private void Start()
        {
            // Subscribe to events of the map 
            OnlineMaps.instance.OnChangePosition += UpdateBubblePosition;
            OnlineMaps.instance.OnChangeZoom += UpdateBubblePosition;
            OnlineMapsControlBase.instance.OnMapClick += OnMapClick;

            if (OnlineMapsControlBaseDynamicMesh.instance != null)
            {
                OnlineMapsControlBaseDynamicMesh.instance.OnMeshUpdated += UpdateBubblePosition;
            }

            if (OnlineMapsCameraOrbit.instance != null)
            {
                OnlineMapsCameraOrbit.instance.OnCameraControl += UpdateBubblePosition;
            }

            if (datas != null) 


        
            foreach (CData data in datas)
                {
                    OnlineMapsMarker marker = OnlineMapsMarkerManager.CreateItem(data.longitude, data.latitude, data.znacznik);
                    marker["data"] = data;
                    marker.texture = data.znacznik;
                    marker.OnClick += OnMarkerClick;
                    
                }




            // Initial hide popup
            bubble.SetActive(false);
        }



        /// <summary>
        /// Updates the popup position
        /// </summary>
        private void UpdateBubblePosition()
        {
            // If no marker is selected then exit.
            if (targetMarker == null) return;

            // Hide the popup if the marker is outside the map view
            if (!targetMarker.inMapView)
            {
                if (bubble.activeSelf) bubble.SetActive(false);
            }
            else if (!bubble.activeSelf) bubble.SetActive(true);

            // Convert the coordinates of the marker to the screen position.
            Vector2 screenPosition = OnlineMapsControlBase.instance.GetScreenPosition(targetMarker.position);

            // Add marker height
            screenPosition.y += targetMarker.height-200;

            // Get a local position inside the canvas.
            Vector2 point;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, screenPosition, null, out point);

            // Set local position of the popup
            (bubble.transform as RectTransform).localPosition = point;
        }
       
        [Serializable]
        public class CData
        {
            public string title;
            public string NotVisited;
            public string Visit;            
            public string describtion;
            public double longitude;
            public double latitude;
            public string wojewodztwo;
            public bool point;
            public Texture2D markerTip;
            public Toggle m_Toggle;
            public int b;
            public Text m_Text;
            public Color disabledColor;
            public Color enabledColor;
            public RawImage acceptedPhoto;
        }

    }
}
Advertisement
void ToggleValueChanged(Toggle change)
{
	m_Text.text = data.Visit;
	m_Text.color =data. enabledColor;
}

void ChangeType(Toggle m_Toggle)
{
	data.m_Toggle.gameObject.SetActive(false);
	data.acceptedPhoto.gameObject.SetActive(true);
}

these are inside the body of

private void OnMarkerClick(OnlineMapsMarkerBase marker)

Like my answer if it was useful

thanks

Neil AE

Indie Game Developer

This topic is closed to new replies.

Advertisement