// Invoke LUIS Unity Event with <GameObject, LUISEntity> private void PerformAction(LUISAction action, GameObject target, LUISEntity entity) { if (action.Response != null) { action.Response.Invoke(target, entity); } }
// Get the normalized value of an entity's list of synonyms private string GetEntityResolutionValue(LUISEntity entity, bool lowerCase = true) { if (entity.resolution == null || entity.resolution.values == null || entity.resolution.values[0] == null) { Debug.LogWarning("No resolution value found for entity: " + entity.entity); return(lowerCase ? entity.entity.ToLower() : entity.entity); } return(lowerCase ? entity.resolution.values[0].ToLower() : entity.resolution.values[0]); }
public void TestChangePosition(string entityProperty) { if (_testTarget == null) { return; } LUISEntity entity = new LUISEntity(); entity.entity = entityProperty; // "forward" for example ChangePosition(_testTarget, entity); }
public void TestChangeSize(string entityProperty) { if (_testTarget == null) { return; } LUISEntity entity = new LUISEntity(); entity.entity = entityProperty; // "smaller" for example ChangeSize(_testTarget, entity); }
public void TestChangeColour(string entityProperty) { if (_testTarget == null) { return; } LUISEntity entity = new LUISEntity(); entity.entity = entityProperty; // "red" for example ChangeColour(_testTarget, entity); }
private LUISEntity GetEntityFromLUISResult(LUISResult result, string entityType) { LUISEntity matchedEntity = null; foreach (LUISEntity entity in result.entities) { if (entity.type.Equals(entityType, ignoreCase) || (entity.resolution != null && entity.resolution.values.Any(s => string.Equals(s, entityType, ignoreCase)))) { matchedEntity = entity; break; } } return(matchedEntity); }
public void ChangeColour(GameObject target, LUISEntity entity) { string color = GetEntityResolutionValue(entity); //entity.entity; Debug.Log("Change color of target: " + target.name + " to color:" + color); Color myColor; if (ColorUtility.TryParseHtmlString(color, out myColor)) { target.GetComponent <Renderer> ().material.color = myColor; } else { Debug.LogWarning("ColorUtility can't parse the color entity: " + color); } }
// NB: In this example all entity positions are handled in a 1st person / VR scenario (taken from the main camera's pov). public void ChangePosition(GameObject target, LUISEntity entity) { string position = GetEntityResolutionValue(entity); Debug.Log("Change position of target: " + target.name + " to position: " + position + " f: " + Camera.main.transform.forward); switch (position) { case "forward": // move the object towards the user target.transform.localPosition -= Camera.main.transform.forward * MoveDistance; break; case "backward": // move the object away from the user target.transform.localPosition += Camera.main.transform.forward * MoveDistance; break; case "left": // move the object to the user's left target.transform.localPosition -= Camera.main.transform.right * MoveDistance; break; case "right": // move the object to the user's right target.transform.localPosition += Camera.main.transform.right * MoveDistance; break; case "up": // move the object higher target.transform.localPosition += Camera.main.transform.up * MoveDistance; break; case "down": // move the object lower target.transform.localPosition -= Camera.main.transform.up * MoveDistance; break; case "center": // move the object into the center of user's current gaze target.transform.localPosition = Camera.main.transform.position + Camera.main.transform.forward * GazeDistance; break; default: Debug.LogWarning("Unhandled position entity: " + position); break; } }
public void ChangeSize(GameObject target, LUISEntity entity) { string size = GetEntityResolutionValue(entity); Debug.Log("Change size of target: " + target.name + " to size:" + size); switch (size) { case "bigger": target.transform.localScale += new Vector3(ScaleAmount, ScaleAmount, ScaleAmount); break; case "smaller": if (target.transform.localScale.x - ScaleAmount > 0 || target.transform.localScale.y - ScaleAmount > 0 || target.transform.localScale.z - ScaleAmount > 0) { target.transform.localScale -= new Vector3(ScaleAmount, ScaleAmount, ScaleAmount); } break; default: Debug.LogWarning("Unhandled size entity: " + size); break; } }
private void OnLUISQueryResult(LUISResult result, GameObject gazedObject) { if (targets == null || actions == null) { Debug.LogWarning("Add some Unity game object targets and actions"); return; } LUISAction action = null; foreach (LUISAction possibleAction in actions) { if (possibleAction.topScoringIntent.Equals(result.topScoringIntent.intent, ignoreCase)) { action = possibleAction; break; } } if (action == null) { Debug.LogWarning("No Unity action matches LUIS intent:" + result.topScoringIntent.intent); return; } // Find target's entity name LUISEntity targetEntity = GetEntityFromLUISResult(result, entityTargetType); if (targetEntity == null) { Debug.LogWarning("Couldn't find any targets for entity type: " + entityTargetType); return; } // Get action entity property LUISEntity entityProperty = GetEntityFromLUISResult(result, action.entityType); if (entityProperty == null) { Debug.LogWarning("No result matches for the action entity type: " + action.entityType); return; } // Only target "this" gameobject if (thisTargetSynonyms.Any(s => string.Equals(s, targetEntity.entity, ignoreCase))) { if (gazedObject == null) { Debug.Log("No gazed object detected for target entity: " + targetEntity.entity); return; } if (GlobalScope) { PerformAction(action, gazedObject, entityProperty); return; } // Check if the gazed object is registered target for LUIS bool isThisALUISTarget = false; foreach (LUISTarget possibleTarget in targets) { if (gazedObject == possibleTarget.target) { isThisALUISTarget = true; Debug.Log("This gazed object is a LUIS target: " + gazedObject.name + " entity:" + entityProperty.entity); PerformAction(action, possibleTarget.target, entityProperty); break; } } if (!isThisALUISTarget) { Debug.LogWarning("This gazed gameObject is not a LUIS target: " + gazedObject.name + " entity:" + entityProperty.entity + "\nTip: Enable GlobalScope to allow LUIS actions to target any gazed GameObject."); } return; } // Target the named gameobject (could be modified to invoke action in multiple matched targets) foreach (LUISTarget possibleTarget in targets) { if (String.Equals(possibleTarget.entityName, targetEntity.entity, ignoreCase) || (targetEntity.resolution != null && targetEntity.resolution.values.Any(s => string.Equals(s, possibleTarget.entityName, ignoreCase)))) { PerformAction(action, possibleTarget.target, entityProperty); break; } else { Debug.Log("Skipped entity name: " + possibleTarget.entityName + " LUIS entity: " + targetEntity.entity); } } }