Esempio n. 1
0
        protected virtual void CheckTriggerExit(GameObject other)
        {
            Usable usable = other.GetComponent <Usable>();

            if (usable != null && usable.enabled)
            {
                if (usablesInRange.Contains(usable))
                {
                    usablesInRange.Remove(usable);
                }
                if (currentUsable == usable)
                {
                    OnDeselectedUsableObject(usable);
                    toldListenersHaveUsable = false;
                    Usable newUsable = null;
                    if (usablesInRange.Count > 0)
                    {
                        newUsable = usablesInRange[0];
                        OnSelectedUsableObject(newUsable);
                        toldListenersHaveUsable = true;
                    }
                    SetCurrentUsable(newUsable);
                }
            }
        }
Esempio n. 2
0
        private void CheckTriggerExit(GameObject other)
        {
            Usable usable = other.GetComponent <Usable>();

            if (usable != null)
            {
                if (usablesInRange.Contains(usable))
                {
                    usablesInRange.Remove(usable);
                }
                if (currentUsable == usable)
                {
                    if (DeselectedUsableObject != null)
                    {
                        DeselectedUsableObject(usable);
                    }
                    onDeselectedUsable.Invoke(usable);
                    Usable newUsable = null;
                    if (usablesInRange.Count > 0)
                    {
                        newUsable = usablesInRange[0];
                        if (SelectedUsableObject != null)
                        {
                            SelectedUsableObject(newUsable);
                        }
                        onSelectedUsable.Invoke(usable);
                    }
                    SetCurrentUsable(newUsable);
                }
            }
        }
 private void OnSelectedUsable(Usable usable)
 {
     this.usable = usable;
     if (usableUI != null)
     {
         usableUI.Hide();                   // Hide previous selection.
     }
     usableUI = (usable != null) ? usable.GetComponentInChildren <AbstractUsableUI>() : null;
     if (usableUI != null)
     {
         usableUI.Show(GetUseMessage());
         HideControls();
     }
     else
     {
         var oldElements = elements;
         SetElementsForUsable(usable);
         if (oldElements != elements)
         {
             var newElements = elements;
             elements = oldElements;
             HideControls();
             elements = newElements;
         }
         ShowControls();
     }
     lastInRange = !IsUsableInRange();
     UpdateDisplay(!lastInRange);
 }
        /// <summary>
        /// If using Standard UI, positions the UI elements on top of the usable target.
        /// </summary>
        public virtual void Update()
        {
            if (selectorUseStandardUIElements == null || !selectorUseStandardUIElements.enabled || StandardUISelectorElements.instance == null)
            {
                return;
            }
            Usable usable = null;

            if (selector != null && selector.enabled)
            {
                usable = selector.CurrentUsable;
            }
            else if (proximitySelector != null && proximitySelector.enabled)
            {
                usable = proximitySelector.CurrentUsable;
            }
            if (usable == null)
            {
                return;
            }
            var mainGraphic = StandardUISelectorElements.instance.mainGraphic;
            var selection   = usable.gameObject;
            var screenPos   = UnityEngine.Camera.main.WorldToScreenPoint(selection.transform.position + (Vector3.up * selectionHeight));

            screenPos += offset;
            screenPos += new Vector3(-mainGraphic.rectTransform.sizeDelta.x / 2, mainGraphic.rectTransform.sizeDelta.y / 2, 0);
            if (screenPos.z < 0)
            {
                return;
            }
            mainGraphic.rectTransform.position = screenPos;
        }
Esempio n. 5
0
        protected void DrawOnSelection(Usable usable, float distance, Selector.Reticle reticle, GUIStyle guiStyle, string defaultUseMessage,
                                       Color inRangeColor, Color outOfRangeColor, TextStyle textStyle, Color textStyleColor)
        {
            if (usable == null)
            {
                return;
            }
            if ((usable != lastUsable) || string.IsNullOrEmpty(heading))
            {
                lastUsable = usable;
                heading    = usable.GetName();
                useMessage = string.IsNullOrEmpty(usable.overrideUseMessage) ? defaultUseMessage : usable.overrideUseMessage;
            }
            GameObject selection = usable.gameObject;

            if (selection != lastSelectionDrawn)
            {
                selectionHeight         = Tools.GetGameObjectHeight(selection);
                selectionHeadingSize    = guiStyle.CalcSize(new GUIContent(heading));
                selectionUseMessageSize = guiStyle.CalcSize(new GUIContent(useMessage));
            }

            // Set text color based on distance:
            bool inUseRange = (distance <= usable.maxUseDistance);

            guiStyle.normal.textColor = inUseRange ? inRangeColor : outOfRangeColor;

            // Draw heading:
            Vector3 screenPos = UnityEngine.Camera.main.WorldToScreenPoint(selection.transform.position + (Vector3.up * selectionHeight));

            screenPos += offset;
            screenPos  = new Vector3(screenPos.x, screenPos.y + selectionUseMessageSize.y + selectionHeadingSize.y, screenPos.z);
            if (screenPos.z < 0)
            {
                return;
            }
            Rect rect = new Rect(screenPos.x - (selectionHeadingSize.x / 2), (Screen.height - screenPos.y) - (selectionHeadingSize.y / 2), selectionHeadingSize.x, selectionHeadingSize.y);

            UnityGUITools.DrawText(rect, heading, guiStyle, textStyle, textStyleColor);

            // Draw use message:
            screenPos  = UnityEngine.Camera.main.WorldToScreenPoint(selection.transform.position + (Vector3.up * (selectionHeight)));
            screenPos += offset;
            screenPos  = new Vector3(screenPos.x, screenPos.y + selectionUseMessageSize.y, screenPos.z);
            rect       = new Rect(screenPos.x - (selectionUseMessageSize.x / 2), (Screen.height - screenPos.y) - (selectionUseMessageSize.y / 2), selectionUseMessageSize.x, selectionUseMessageSize.y);
            UnityGUITools.DrawText(rect, useMessage, guiStyle, textStyle, textStyleColor);

            // Draw reticle:
            if (reticle != null)
            {
                Texture2D reticleTexture = inUseRange ? reticle.inRange : reticle.outOfRange;
                if (reticleTexture != null)
                {
                    screenPos = UnityEngine.Camera.main.WorldToScreenPoint(selection.transform.position + (Vector3.up * 0.5f * selectionHeight));
                    rect      = new Rect(screenPos.x - (reticle.width / 2), (Screen.height - screenPos.y) - (reticle.height / 2), reticle.width, reticle.height);
                    GUI.Label(rect, reticleTexture);
                }
            }
        }
Esempio n. 6
0
 protected void OnSelectedUsableObject(Usable usable)
 {
     if (SelectedUsableObject != null)
     {
         SelectedUsableObject(usable);
     }
     onSelectedUsable.Invoke(usable);
 }
Esempio n. 7
0
 protected void OnDeselectedUsableObject(Usable usable)
 {
     if (DeselectedUsableObject != null)
     {
         DeselectedUsableObject(usable);
     }
     onDeselectedUsable.Invoke(usable);
 }
Esempio n. 8
0
 protected virtual void DeselectTarget()
 {
     OnDeselectedUsableObject(usable);
     usable     = null;
     selection  = null;
     heading    = string.Empty;
     useMessage = string.Empty;
 }
Esempio n. 9
0
        protected virtual void CheckTriggerExit(GameObject other)
        {
            Usable usable = other.GetComponent <Usable>();

            if (usable != null && usable.enabled)
            {
                RemoveUsableFromDetectedList(usable);
            }
        }
 private void OnDeselectedUsable(Usable usable)
 {
     if (usableUI != null)
     {
         usableUI.Hide();
         usableUI = null;
     }
     HideControls();
     this.usable = null;
 }
Esempio n. 11
0
 private void DeselectTarget()
 {
     if ((usable != null) && (DeselectedUsableObject != null))
     {
         DeselectedUsableObject(usable);
     }
     usable     = null;
     selection  = null;
     heading    = string.Empty;
     useMessage = string.Empty;
 }
Esempio n. 12
0
        public void Start()
        {
            Usable usable = Tools.GetComponentAnywhere <Usable>(gameObject);

            if ((usable != null) && (nameText != null))
            {
                nameText.text = usable.GetName();
            }
            if (canvas != null)
            {
                canvas.enabled = false;
            }
        }
 private void OnDeselectedUsable(Usable usable)
 {
     if (usableUnityUI != null)
     {
         usableUnityUI.Hide();
         usableUnityUI = null;
     }
     else
     {
         HideControls();
     }
     this.usable = null;
 }
Esempio n. 14
0
 protected virtual void SetCurrentUsable(Usable usable)
 {
     currentUsable = usable;
     if (usable != null)
     {
         currentHeading    = currentUsable.GetName();
         currentUseMessage = DialogueManager.GetLocalizedText(string.IsNullOrEmpty(currentUsable.overrideUseMessage) ? defaultUseMessage : currentUsable.overrideUseMessage);
     }
     else
     {
         currentHeading    = string.Empty;
         currentUseMessage = string.Empty;
     }
 }
Esempio n. 15
0
 private void SetCurrentUsable(Usable usable)
 {
     currentUsable = usable;
     if (usable != null)
     {
         currentHeading    = currentUsable.GetName();
         currentUseMessage = string.IsNullOrEmpty(currentUsable.overrideUseMessage) ? defaultUseMessage : currentUsable.overrideUseMessage;
     }
     else
     {
         currentHeading    = string.Empty;
         currentUseMessage = string.Empty;
     }
 }
Esempio n. 16
0
        protected virtual void CheckTriggerEnter(GameObject other)
        {
            Usable usable = other.GetComponent <Usable>();

            if (usable != null && usable.enabled)
            {
                SetCurrentUsable(usable);
                if (!usablesInRange.Contains(usable))
                {
                    usablesInRange.Add(usable);
                }
                OnSelectedUsableObject(usable);
                toldListenersHaveUsable = true;
            }
        }
 private void OnSelectedUsable(Usable usable)
 {
     this.usable   = usable;
     usableUnityUI = (usable != null) ? usable.GetComponentInChildren <UsableUnityUI>() : null;
     if (usableUnityUI != null)
     {
         usableUnityUI.Show(GetUseMessage());
     }
     else
     {
         ShowControls();
     }
     lastInRange = !IsUsableInRange();
     UpdateDisplay(!lastInRange);
 }
Esempio n. 18
0
 protected virtual void DeselectTarget()
 {
     if (usable != null)
     {
         if (DeselectedUsableObject != null)
         {
             DeselectedUsableObject(usable);
         }
         onDeselectedUsable.Invoke(usable);
     }
     usable     = null;
     selection  = null;
     heading    = string.Empty;
     useMessage = string.Empty;
 }
Esempio n. 19
0
 public virtual void SetCurrentUsable(Usable usable)
 {
     if (usable == null)
     {
         DeselectTarget();
     }
     else
     {
         this.usable = usable;
         selection   = usable.gameObject;
         heading     = string.Empty;
         useMessage  = string.Empty;
         OnSelectedUsableObject(usable);
     }
 }
Esempio n. 20
0
        private void CheckTriggerEnter(GameObject other)
        {
            Usable usable = other.GetComponent <Usable>();

            if (usable != null)
            {
                SetCurrentUsable(usable);
                if (!usablesInRange.Contains(usable))
                {
                    usablesInRange.Add(usable);
                }
                if (SelectedUsableObject != null)
                {
                    SelectedUsableObject(usable);
                }
            }
        }
Esempio n. 21
0
        private bool DrawTargetingOnUse()
        {
            EditorWindowTools.DrawHorizontalLine();
            EditorGUILayout.LabelField("Collider", EditorStyles.boldLabel);
            Collider nontriggerCollider = null;

            foreach (var collider in npcObject.GetComponentsInChildren <Collider>())
            {
                if (!collider.isTrigger)
                {
                    nontriggerCollider = collider;
                }
            }
            Usable usable = npcObject.GetComponent <Usable>();

            if ((nontriggerCollider != null) && (usable != null))
            {
                EditorGUILayout.HelpBox("The NPC has a collider and a Usable component. The player's Selector component will be able to target it to send OnUse messages.", MessageType.None);
                EditorGUILayout.LabelField("'Usable' Customization", EditorStyles.boldLabel);
                EditorWindowTools.StartIndentedSection();
                usable.maxUseDistance     = EditorGUILayout.FloatField("Max Usable Distance", usable.maxUseDistance);
                usable.overrideName       = EditorGUILayout.TextField("Override Actor Name (leave blank to use main override)", usable.overrideName);
                usable.overrideUseMessage = EditorGUILayout.TextField("Override Use Message", usable.overrideUseMessage);
                EditorWindowTools.EndIndentedSection();
            }
            else
            {
                if (nontriggerCollider == null)
                {
                    EditorGUILayout.HelpBox("The NPC is configured to listen for OnUse messages. If these messages will be coming from the player's Selector component, the NPC needs a collider that the Selector can target. Click Add Collider to add a CharacterController. If OnUse will come from another source, you don't necessarily need a collider.", MessageType.Info);
                    if (GUILayout.Button("Add Collider", GUILayout.Width(160)))
                    {
                        npcObject.AddComponent <CharacterController>();
                    }
                }
                if (usable == null)
                {
                    EditorGUILayout.HelpBox("The NPC is configured to listen for OnUse messages. If these messages will be coming from the player's Selector component, the NPC needs a Usable component to tell the Selector that it's usable. Click Add Usable to add one.", MessageType.Info);
                    if (GUILayout.Button("Add Usable", GUILayout.Width(160)))
                    {
                        npcObject.AddComponent <Usable>();
                    }
                }
            }
            return(true);
        }
Esempio n. 22
0
 // Use this for initialization
 void Start()
 {
     try{
         this.zorrilla = GameObject.Find("Zorrilla").GetComponent <PixelCrushers.DialogueSystem.Usable>();
     }catch {}
     try{
         this.lucrecia = GameObject.Find("Lucrecia").GetComponent <PixelCrushers.DialogueSystem.Usable>();
     }catch {}
     try{
         this.claudio = GameObject.Find("Claudio").GetComponent <PixelCrushers.DialogueSystem.Usable>();
     }catch {}
     try{
         this.leonarda = GameObject.Find("Leonarda").GetComponent <PixelCrushers.DialogueSystem.Usable>();
     }catch {}
     try{
         this.marcelo = GameObject.Find("Marcelo").GetComponent <PixelCrushers.DialogueSystem.Usable>();
     }catch {}
 }
 // Use this for initialization
 void Start()
 {
     try{
         this.zorrilla = GameObject.Find ("Zorrilla").GetComponent<PixelCrushers.DialogueSystem.Usable>();
     }catch{}
     try{
         this.lucrecia = GameObject.Find ("Lucrecia").GetComponent<PixelCrushers.DialogueSystem.Usable>();
     }catch{}
     try{
         this.claudio = GameObject.Find ("Claudio").GetComponent<PixelCrushers.DialogueSystem.Usable>();
     }catch{}
     try{
         this.leonarda = GameObject.Find ("Leonarda").GetComponent<PixelCrushers.DialogueSystem.Usable>();
     }catch{}
     try{
         this.marcelo = GameObject.Find ("Marcelo").GetComponent<PixelCrushers.DialogueSystem.Usable>();
     }catch{}
 }
        private void SetElementsForUsable(Usable usable)
        {
            // Check tag-specific UI elements:
            for (int i = 0; i < tagSpecificElements.Count; i++)
            {
                var tagInfo = tagSpecificElements[i];
                if (usable != null && usable.CompareTag(tagInfo.tag))
                {
                    defaultUseMessage = tagInfo.defaultUseMessage;
                    elements          = tagInfo.UIElements;
                    return;
                }
            }

            // Check layer-specific UI elements:
            for (int i = 0; i < layerSpecificElements.Count; i++)
            {
                var layerInfo = layerSpecificElements[i];
                if (usable != null && ((1 << usable.gameObject.layer) & layerInfo.layerMask.value) != 0)
                {
                    defaultUseMessage = layerInfo.defaultUseMessage;
                    elements          = layerInfo.UIElements;
                    return;
                }
            }

            // Otherwise get default elements:
            defaultUseMessage = originalDefaultUseMessage;
            if (layerSpecificElements.Count > 0 || tagSpecificElements.Count > 0)
            {
                for (int i = 0; i < StandardUISelectorElements.instances.Count; i++)
                {
                    var instance   = StandardUISelectorElements.instances[i];
                    var isSpecific = (layerSpecificElements.Find(x => x.UIElements == instance) != null) ||
                                     (tagSpecificElements.Find(x => x.UIElements == instance) != null);
                    if (!isSpecific)
                    {
                        elements = instance;
                        return;
                    }
                }
            }
            elements = StandardUISelectorElements.instance;
        }
		protected void DrawOnSelection(Usable usable, float distance, Selector.Reticle reticle, GUIStyle guiStyle, string defaultUseMessage,
		                               Color inRangeColor, Color outOfRangeColor, TextStyle textStyle, Color textStyleColor) {
			if (usable == null) return;
			if ((usable != lastUsable) || string.IsNullOrEmpty(heading)) {
				lastUsable = usable;
				heading = usable.GetName();
				useMessage = string.IsNullOrEmpty(usable.overrideUseMessage) ? defaultUseMessage : usable.overrideUseMessage;
			}
			GameObject selection = usable.gameObject;
			if (selection != lastSelectionDrawn) {
				selectionHeight = Tools.GetGameObjectHeight(selection);
				selectionHeadingSize = guiStyle.CalcSize(new GUIContent(heading));
				selectionUseMessageSize = guiStyle.CalcSize(new GUIContent(useMessage));
			}

			// Set text color based on distance:
			bool inUseRange = (distance <= usable.maxUseDistance);
			guiStyle.normal.textColor = inUseRange ? inRangeColor : outOfRangeColor;

			// Draw heading:
			Vector3 screenPos = Camera.main.WorldToScreenPoint(selection.transform.position + (Vector3.up * selectionHeight));
			screenPos += offset;
			screenPos = new Vector3(screenPos.x, screenPos.y + selectionUseMessageSize.y + selectionHeadingSize.y, screenPos.z);
			if (screenPos.z < 0) return;
			Rect rect = new Rect(screenPos.x - (selectionHeadingSize.x / 2), (Screen.height - screenPos.y) - (selectionHeadingSize.y / 2), selectionHeadingSize.x, selectionHeadingSize.y);
			UnityGUITools.DrawText(rect, heading, guiStyle, textStyle, textStyleColor);
			
			// Draw use message:
			screenPos = Camera.main.WorldToScreenPoint(selection.transform.position + (Vector3.up * (selectionHeight)));
			screenPos += offset;
			screenPos = new Vector3(screenPos.x, screenPos.y + selectionUseMessageSize.y, screenPos.z);
			rect = new Rect(screenPos.x - (selectionUseMessageSize.x / 2), (Screen.height - screenPos.y) - (selectionUseMessageSize.y / 2), selectionUseMessageSize.x, selectionUseMessageSize.y);
			UnityGUITools.DrawText(rect, useMessage, guiStyle, textStyle, textStyleColor);
			
			// Draw reticle:
			if (reticle != null) {
				Texture2D reticleTexture = inUseRange ? reticle.inRange : reticle.outOfRange;
				if (reticleTexture != null) {
					screenPos = Camera.main.WorldToScreenPoint(selection.transform.position + (Vector3.up * 0.5f * selectionHeight));
					rect = new Rect(screenPos.x - (reticle.width / 2), (Screen.height - screenPos.y) - (reticle.height / 2), reticle.width, reticle.height);
					GUI.Label(rect, reticleTexture);
				}
			}
		}
Esempio n. 26
0
 public virtual void RemoveUsableFromDetectedList(Usable usable)
 {
     if (usable != null)
     {
         if (usablesInRange.Contains(usable))
         {
             usablesInRange.Remove(usable);
         }
         if (currentUsable == usable)
         {
             OnDeselectedUsableObject(usable);
             toldListenersHaveUsable = false;
             Usable newUsable = null;
             usablesInRange.RemoveAll(x => x == null || !x.gameObject.activeInHierarchy);
             if (usablesInRange.Count > 0)
             {
                 newUsable = usablesInRange[0];
                 OnSelectedUsableObject(newUsable);
                 toldListenersHaveUsable = true;
             }
             SetCurrentUsable(newUsable);
         }
     }
 }
		private void OnSelectedUsable(Usable usable) {
			this.usable = usable;
			usableUnityUI = (usable != null) ? usable.GetComponentInChildren<UsableUnityUI>() : null;
			if (usableUnityUI != null) {
				usableUnityUI.Show(GetUseMessage());
			} else {
				ShowControls();
			}
			lastInRange = !IsUsableInRange();
			UpdateDisplay(!lastInRange);
		}
Esempio n. 28
0
        protected virtual void Run3DRaycast()
        {
            Ray ray = UnityEngine.Camera.main.ScreenPointToRay(GetSelectionPoint());

            lastRay = ray;

            // New Variable rayCastDistance is used below for the raycasts instead of maxSelectionDistance to be able to set it to infinity (if using DistanceFrom.GameObject) instead of maxSelectionDistance:
            // Credit: Daniel D. (Thank you!)
            float raycastDistance = (distanceFrom == DistanceFrom.GameObject) ? Mathf.Infinity : maxSelectionDistance;

            if (raycastAll)
            {
                // Run RaycastAll:
                if (lastHits == null)
                {
                    lastHits = new RaycastHit[MaxHits];
                }
                numLastHits = Physics.RaycastNonAlloc(ray, lastHits, raycastDistance, layerMask);
                bool foundUsable = false;
                for (int i = 0; i < numLastHits; i++)
                {
                    var   hit         = lastHits[i];
                    float hitDistance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection == hit.collider.gameObject)
                    {
                        foundUsable = true;
                        distance    = hitDistance;
                        break;
                    }
                    else
                    {
                        Usable hitUsable = hit.collider.GetComponent <Usable>();
                        if (hitUsable != null && hitUsable.enabled && hitDistance <= maxSelectionDistance)
                        {
                            foundUsable = true;
                            distance    = hitDistance;
                            usable      = hitUsable;
                            selection   = hit.collider.gameObject;
                            OnSelectedUsableObject(usable);
                            break;
                        }
                    }
                }
                if (!foundUsable)
                {
                    DeselectTarget();
                }
            }
            else
            {
                // Cast a ray and see what we hit:
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, maxSelectionDistance, layerMask))
                {
                    distance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    Usable hitUsable = hit.collider.GetComponent <Usable>();
                    if (hitUsable != null && hitUsable.enabled)
                    {
                        if (selection != hit.collider.gameObject)
                        {
                            usable     = hitUsable;
                            selection  = hit.collider.gameObject;
                            heading    = string.Empty;
                            useMessage = string.Empty;
                            OnSelectedUsableObject(usable);
                        }
                    }
                    else
                    {
                        DeselectTarget();
                    }
                }
                else
                {
                    DeselectTarget();
                }
                lastHit = hit;
            }
        }
Esempio n. 29
0
        protected virtual void Run2DRaycast()
        {
#if USE_PHYSICS2D || !UNITY_2018_1_OR_NEWER
            if (raycastAll)
            {
                // Run Physics2D.RaycastAll:
                if (lastHits2D == null)
                {
                    lastHits2D = new RaycastHit2D[MaxHits];
                }
                int  numHits     = Physics2D.RaycastNonAlloc(UnityEngine.Camera.main.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, lastHits2D, maxSelectionDistance, layerMask);
                bool foundUsable = false;
                for (int i = 0; i < numHits; i++)
                {
                    var   hit         = lastHits2D[i];
                    float hitDistance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection == hit.collider.gameObject)
                    {
                        foundUsable = true;
                        distance    = hitDistance;
                        break;
                    }
                    else
                    {
                        Usable hitUsable = hit.collider.GetComponent <Usable>();
                        if (hitUsable != null && hitUsable.enabled)
                        {
                            foundUsable = true;
                            distance    = hitDistance;
                            usable      = hitUsable;
                            selection   = hit.collider.gameObject;
                            OnSelectedUsableObject(usable);
                            break;
                        }
                    }
                }
                if (!foundUsable)
                {
                    DeselectTarget();
                }
            }
            else
            {
                // Cast a ray and see what we hit:
                RaycastHit2D hit;
                hit = Physics2D.Raycast(UnityEngine.Camera.main.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, maxSelectionDistance, layerMask);
                if (hit.collider != null)
                {
                    distance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection != hit.collider.gameObject)
                    {
                        Usable hitUsable = hit.collider.GetComponent <Usable>();
                        if (hitUsable != null && hitUsable.enabled)
                        {
                            usable     = hitUsable;
                            selection  = hit.collider.gameObject;
                            heading    = string.Empty;
                            useMessage = string.Empty;
                            OnSelectedUsableObject(usable);
                        }
                        else
                        {
                            DeselectTarget();
                        }
                    }
                }
                else
                {
                    DeselectTarget();
                }
            }
#endif
        }
Esempio n. 30
0
		private void Run2DRaycast() {
			if (raycastAll) {
				
				// Run Physics2D.RaycastAll:
				RaycastHit2D[] hits;
				hits = Physics2D.RaycastAll(UnityEngine.Camera.main.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, maxSelectionDistance, layerMask);
				bool foundUsable = false;
				foreach (var hit in hits) {
					float hitDistance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
					if (selection == hit.collider.gameObject) {
						foundUsable = true;
						distance = hitDistance;
						break;
					} else {
						Usable hitUsable = hit.collider.gameObject.GetComponent<Usable>();
						if (hitUsable != null) {
							foundUsable = true;
							distance = hitDistance;
							usable = hitUsable;
							selection = hit.collider.gameObject;
							if (SelectedUsableObject != null) SelectedUsableObject(usable);
							break;
						}
					}
				}
				if (!foundUsable) {
					DeselectTarget();
				}
				
			} else {
				
				// Cast a ray and see what we hit:
				RaycastHit2D hit;
				hit = Physics2D.Raycast(UnityEngine.Camera.main.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, maxSelectionDistance, layerMask);
				if (hit.collider != null) {
					distance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
					if (selection != hit.collider.gameObject) {
						Usable hitUsable = hit.collider.gameObject.GetComponent<Usable>();
						if (hitUsable != null) {
							usable = hitUsable;
							selection = hit.collider.gameObject;
							heading = string.Empty;
							useMessage = string.Empty;
							if (SelectedUsableObject != null) SelectedUsableObject(usable);
						} else {
							DeselectTarget();
						}
					}
				} else {
					DeselectTarget();
				}
			}
		}
Esempio n. 31
0
        private void Run3DRaycast()
        {
            Ray ray = UnityEngine.Camera.main.ScreenPointToRay(GetSelectionPoint());

            lastRay = ray;

            if (raycastAll)
            {
                // Run RaycastAll:
                RaycastHit[] hits;
                hits     = Physics.RaycastAll(ray, maxSelectionDistance, layerMask);
                lastRay  = ray;
                lastHits = hits;
                bool foundUsable = false;
                foreach (var hit in hits)
                {
                    float hitDistance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection == hit.collider.gameObject)
                    {
                        foundUsable = true;
                        distance    = hitDistance;
                        break;
                    }
                    else
                    {
                        Usable hitUsable = hit.collider.gameObject.GetComponent <Usable>();
                        if (hitUsable != null)
                        {
                            foundUsable = true;
                            distance    = hitDistance;
                            usable      = hitUsable;
                            selection   = hit.collider.gameObject;
                            if (SelectedUsableObject != null)
                            {
                                SelectedUsableObject(usable);
                            }
                            break;
                        }
                    }
                }
                if (!foundUsable)
                {
                    DeselectTarget();
                }
            }
            else
            {
                // Cast a ray and see what we hit:
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, maxSelectionDistance, layerMask))
                {
                    distance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection != hit.collider.gameObject)
                    {
                        Usable hitUsable = hit.collider.gameObject.GetComponent <Usable>();
                        if (hitUsable != null)
                        {
                            usable     = hitUsable;
                            selection  = hit.collider.gameObject;
                            heading    = string.Empty;
                            useMessage = string.Empty;
                            if (SelectedUsableObject != null)
                            {
                                SelectedUsableObject(usable);
                            }
                        }
                        else
                        {
                            DeselectTarget();
                        }
                    }
                }
                else
                {
                    DeselectTarget();
                }
                lastHit = hit;
            }
        }
Esempio n. 32
0
        protected virtual void Run2DRaycast()
        {
#if USE_PHYSICS2D || !UNITY_2018_1_OR_NEWER
            var mainCamera = UnityEngine.Camera.main;
            if (!mainCamera.orthographic && !hasReportedInvalidCamera)
            {
                hasReportedInvalidCamera = true;
                Debug.LogWarning("In 2D mode, Selector requires an orthographic camera.", this);
            }

            if (raycastAll)
            {
                // Run Physics2D.RaycastAll:
                if (lastHits2D == null)
                {
                    lastHits2D = new RaycastHit2D[MaxHits];
                }
                int  numHits     = Physics2D.RaycastNonAlloc(mainCamera.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, lastHits2D, maxSelectionDistance, layerMask);
                bool foundUsable = false;
                for (int i = 0; i < numHits; i++)
                {
                    var   hit         = lastHits2D[i];
                    float hitDistance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection == hit.collider.gameObject)
                    {
                        foundUsable = true;
                        distance    = hitDistance;
                        break;
                    }
                    else
                    {
                        Usable hitUsable = hit.collider.GetComponent <Usable>();
                        if (hitUsable != null && hitUsable.enabled)
                        {
                            foundUsable = true;
                            distance    = hitDistance;
                            SetCurrentUsable(hitUsable);
                            break;
                        }
                    }
                }
                if (!foundUsable)
                {
                    DeselectTarget();
                }
            }
            else
            {
                // Cast a ray and see what we hit:
                RaycastHit2D hit;
                hit = Physics2D.Raycast(mainCamera.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, maxSelectionDistance, layerMask);
                if (hit.collider != null)
                {
                    distance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection != hit.collider.gameObject)
                    {
                        Usable hitUsable = hit.collider.GetComponent <Usable>();
                        if (hitUsable != null && hitUsable.enabled)
                        {
                            heading    = string.Empty;
                            useMessage = string.Empty;
                            SetCurrentUsable(hitUsable);
                        }
                        else
                        {
                            DeselectTarget();
                        }
                    }
                }
                else
                {
                    DeselectTarget();
                }
            }
#endif
        }
Esempio n. 33
0
        protected virtual void Run3DRaycast()
        {
            Ray ray = UnityEngine.Camera.main.ScreenPointToRay(GetSelectionPoint());

            lastRay = ray;

            if (raycastAll)
            {
                // Run RaycastAll:
                if (lastHits == null)
                {
                    lastHits = new RaycastHit[MaxHits];
                }
                numLastHits = Physics.RaycastNonAlloc(ray, lastHits, maxSelectionDistance, layerMask);
                bool foundUsable = false;
                for (int i = 0; i < numLastHits; i++)
                {
                    var   hit         = lastHits[i];
                    float hitDistance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection == hit.collider.gameObject)
                    {
                        foundUsable = true;
                        distance    = hitDistance;
                        break;
                    }
                    else
                    {
                        Usable hitUsable = hit.collider.gameObject.GetComponent <Usable>();
                        if (hitUsable != null && hitUsable.enabled)
                        {
                            foundUsable = true;
                            distance    = hitDistance;
                            usable      = hitUsable;
                            selection   = hit.collider.gameObject;
                            OnSelectedUsableObject(usable);
                            break;
                        }
                    }
                }
                if (!foundUsable)
                {
                    DeselectTarget();
                }
            }
            else
            {
                // Cast a ray and see what we hit:
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, maxSelectionDistance, layerMask))
                {
                    distance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
                    if (selection != hit.collider.gameObject)
                    {
                        Usable hitUsable = hit.collider.gameObject.GetComponent <Usable>();
                        if (hitUsable != null && hitUsable.enabled)
                        {
                            usable     = hitUsable;
                            selection  = hit.collider.gameObject;
                            heading    = string.Empty;
                            useMessage = string.Empty;
                            OnSelectedUsableObject(usable);
                        }
                        else
                        {
                            DeselectTarget();
                        }
                    }
                }
                else
                {
                    DeselectTarget();
                }
                lastHit = hit;
            }
        }
Esempio n. 34
0
		private void Run3DRaycast() {
			Ray ray = UnityEngine.Camera.main.ScreenPointToRay(GetSelectionPoint());
			lastRay = ray;
			
			if (raycastAll) {
				
				// Run RaycastAll:
				RaycastHit[] hits;
				hits = Physics.RaycastAll(ray, maxSelectionDistance, layerMask);
				lastRay = ray;
				lastHits = hits;
				bool foundUsable = false;
				foreach (var hit in hits) {
					float hitDistance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
					if (selection == hit.collider.gameObject) {
						foundUsable = true;
						distance = hitDistance;
						break;
					} else {
						Usable hitUsable = hit.collider.gameObject.GetComponent<Usable>();
						if (hitUsable != null) {
							foundUsable = true;
							distance = hitDistance;
							usable = hitUsable;
							selection = hit.collider.gameObject;
							if (SelectedUsableObject != null) SelectedUsableObject(usable);
							break;
						}
					}
				}
				if (!foundUsable) {
					DeselectTarget();
				}
				
			} else {
				
				// Cast a ray and see what we hit:
				RaycastHit hit;
				if (Physics.Raycast(ray, out hit, maxSelectionDistance, layerMask)) {
					distance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
					if (selection != hit.collider.gameObject) {
						Usable hitUsable = hit.collider.gameObject.GetComponent<Usable>();
						if (hitUsable != null) {
							usable = hitUsable;
							selection = hit.collider.gameObject;
							heading = string.Empty;
							useMessage = string.Empty;
							if (SelectedUsableObject != null) SelectedUsableObject(usable);
						} else {
							DeselectTarget();
						}
					}
				} else {
					DeselectTarget();
				}
				lastHit = hit;
			}
		}
		private void OnDeselectedUsable(Usable usable) {
			if (usableUnityUI != null) {
				usableUnityUI.Hide();
				usableUnityUI = null;
			} else {
				HideControls();
			}
			this.usable = null;
		}
Esempio n. 36
0
		private void DeselectTarget() {
			if ((usable != null) && (DeselectedUsableObject != null)) DeselectedUsableObject(usable);
			usable = null;
			selection = null;
			heading = string.Empty;
			useMessage = string.Empty;
		}
Esempio n. 37
0
		private void OnDeselectedUsable(Usable usable) {
			HideControls();
			this.usable = null;
		}
Esempio n. 38
0
		private void OnSelectedUsable(Usable usable) {
			this.usable = usable;
			if (mainControl != null) NGUITools.SetActive(mainControl.gameObject, true);
			if (nameLabel != null) {
				NGUITools.SetActive(nameLabel.gameObject, true);
				nameLabel.text = usable.GetName();
			}
			if (useMessageLabel != null) {
				NGUITools.SetActive(useMessageLabel.gameObject, true);
				useMessageLabel.text = string.IsNullOrEmpty(usable.overrideUseMessage) ? defaultUseMessage : usable.overrideUseMessage;
			}
			UpdateReticle();
		}
		private void SetCurrentUsable(Usable usable) {
			currentUsable = usable;
			if (usable != null) {
				currentHeading = currentUsable.GetName();
				currentUseMessage = string.IsNullOrEmpty(currentUsable.overrideUseMessage) ? defaultUseMessage : currentUsable.overrideUseMessage;
			} else {
				currentHeading = string.Empty;
				currentUseMessage = string.Empty;
			}
		}
Esempio n. 40
0
		/// <summary>
		/// Runs a raycast to see what's under the selection point. Updates the selection and
		/// calls the selection delegates if the selection has changed. If the player hits the
		/// use button, sends an OnUse message to the selection.
		/// </summary>
		void Update() {
			// Exit if disabled or paused:
			if (!enabled || (Time.timeScale <= 0)) return;
			
			// Exit if there's no camera:
			if (Camera.main == null) return;

			Ray ray = Camera.main.ScreenPointToRay(GetSelectionPoint());
			lastRay = ray;

			if (raycastAll) {

				// Run RaycastAll:
				RaycastHit[] hits;
				hits = Physics.RaycastAll(ray, maxSelectionDistance, layerMask);
				lastRay = ray;
				lastHits = hits;
				bool foundUsable = false;
				foreach (var hit in hits) {
					float hitDistance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
					if (selection == hit.collider.gameObject) {
						foundUsable = true;
						distance = hitDistance;
						break;
					} else {
						Usable hitUsable = hit.collider.gameObject.GetComponent<Usable>();
						if (hitUsable != null) {
							foundUsable = true;
							distance = hitDistance;
							usable = hitUsable;
							selection = hit.collider.gameObject;
							if (SelectedUsableObject != null) SelectedUsableObject(usable);
							break;
						}
					}
				}
				if (!foundUsable) {
					DeselectTarget();
				}

			} else {
			
				// Cast a ray and see what we hit:
				RaycastHit hit;
				if (Physics.Raycast(ray, out hit, maxSelectionDistance, layerMask)) {
					distance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
					if (selection != hit.collider.gameObject) {
						Usable hitUsable = hit.collider.gameObject.GetComponent<Usable>();
						if (hitUsable != null) {
							usable = hitUsable;
							selection = hit.collider.gameObject;
							heading = string.Empty;
							useMessage = string.Empty;
							if (SelectedUsableObject != null) SelectedUsableObject(usable);
						} else {
							DeselectTarget();
						}
					}
				} else {
					DeselectTarget();
				}
				lastHit = hit;
			}
			
			// If the player presses the use key/button, send the OnUse message:
			if (IsUseButtonDown() && (usable != null) && (distance <= usable.maxUseDistance)) {
				if (broadcastToChildren) {
					usable.gameObject.BroadcastMessage("OnUse", this.transform, SendMessageOptions.DontRequireReceiver);
				} else {
					usable.gameObject.SendMessage("OnUse", this.transform, SendMessageOptions.DontRequireReceiver);
				}
			}
		}
Esempio n. 41
0
 private void Run2DRaycast()
 {
     if (raycastAll)
     {
         // Run Physics2D.RaycastAll:
         RaycastHit2D[] hits;
         hits = Physics2D.RaycastAll(UnityEngine.Camera.main.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, maxSelectionDistance, layerMask);
         bool foundUsable = false;
         foreach (var hit in hits)
         {
             float hitDistance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
             if (selection == hit.collider.gameObject)
             {
                 foundUsable = true;
                 distance    = hitDistance;
                 break;
             }
             else
             {
                 Usable hitUsable = hit.collider.gameObject.GetComponent <Usable>();
                 if (hitUsable != null)
                 {
                     foundUsable = true;
                     distance    = hitDistance;
                     usable      = hitUsable;
                     selection   = hit.collider.gameObject;
                     if (SelectedUsableObject != null)
                     {
                         SelectedUsableObject(usable);
                     }
                     break;
                 }
             }
         }
         if (!foundUsable)
         {
             DeselectTarget();
         }
     }
     else
     {
         // Cast a ray and see what we hit:
         RaycastHit2D hit;
         hit = Physics2D.Raycast(UnityEngine.Camera.main.ScreenToWorldPoint(GetSelectionPoint()), Vector2.zero, maxSelectionDistance, layerMask);
         if (hit.collider != null)
         {
             distance = (distanceFrom == DistanceFrom.Camera) ? 0 : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position);
             if (selection != hit.collider.gameObject)
             {
                 Usable hitUsable = hit.collider.gameObject.GetComponent <Usable>();
                 if (hitUsable != null)
                 {
                     usable     = hitUsable;
                     selection  = hit.collider.gameObject;
                     heading    = string.Empty;
                     useMessage = string.Empty;
                     if (SelectedUsableObject != null)
                     {
                         SelectedUsableObject(usable);
                     }
                 }
                 else
                 {
                     DeselectTarget();
                 }
             }
         }
         else
         {
             DeselectTarget();
         }
     }
 }