TouchReceiveComponent

The Entity becomes touchable, so you can control its behavior when touching it.

# Properties

[{"index":"boolean AutoFitOnce (MakerOnly)","0":"Automatically changes TouchArea and Offset values based on the scale value of AvatarRendererComponent or SpriteRendererComponent when set to true. This only changes the very first time."}]

[{"index":"boolean AutoFitToSize","0":"If set to true, the TouchArea and Offset values ​​will automatically change whenever the Scale value of the AvatarRendererComponent or SpriteRendererComponent changes."}]

[{"index":"boolean DynamicTouchArea (Deprecated)","0":"This property is deprecated. Use the AutoFitToSize property."}]

[{"index":"Vector2 Offset","0":"Sets the centerpoint of the touch area."}]

[{"index":"boolean RelayEventToBehind","0":"Sets whether or not to generate a touch event in the TouchReceiveComponent located behind in the rendering order."}]

[{"index":"Vector2 TouchArea","0":"The size of the touch area. The value of TouchArea may change in the middle when AutoFitToSize is true."}]

[{"index":"float TouchAreaUpdateTime (Deprecated)","0":"This is a deprecated property. Use the AutoFitToSize property."}]

##### inherited from Component:

[{"index":"boolean Enable (Sync)","0":"Checks whether Component is activated or not."}]

[{"index":"boolean EnableInHierarchy (ReadOnly) (HideFromInspector)","0":"Displays whether this Component is Enable in the hierarchy. If Entity Enable is false regardless of Component Enable, returns false."}]

[{"index":"Entity Entity (ReadOnly)","0":"The Entity that owns this component."}]

# Methods

##### inherited from Component:

[{"index":"boolean IsClient()","0":"Returns whether the current execution environment is a client or not."}]

[{"index":"boolean IsServer()","0":"Returns whether the current execution environment is a server or not."}]

# Events

[{"index":"TouchEvent","0":"This event occurs when an entity is touched. The entity must have a TouchReceiveComponent for the event to occur."}]

[{"index":"TouchHoldEvent","0":"This event occurs when an entity is touched. The entity must have a TouchReceiveComponent for the event to occur. Cannot be triggered by a short touch."}]

[{"index":"TouchReleaseEvent","0":"This event occurs when an entity is released from the touch. The entity must have a TouchReceiveComponent for the event to occur. Cannot be triggered by a short touch."}]

# Examples

#### TouchEvent

This example demonstrates how to display a specific UI when touching an NPC. This kind of process is useful for various purposes like talking or trading with NPCs.
    
    
    @Component
    script UITouchDisplay extends Component
        @EventSender("Self")
        handler HandleTouchEvent(TouchEvent event)
        -- Parameters
        --------------------------------------------------------
    
        local uiPath = "/ui/DefaultGroup/ExampleUI"
        local targetUI = _EntityService:GetEntityByPath(uiPath)
    
        targetUI:SetVisible(true)
        end
    end
    

#### TouchHoldEvent

This is an example where the player touches the entity and moves it. TouchReceiveComponent must be added to an entity.
    
    
    @Component
    script TouchDragger extends Component
        property table Index = {}
        property any Handler = nil
    
        @ExecSpace("ClientOnly")
        method void Move(any screenholdEvent)
        local touchId = screenholdEvent.TouchId
        local touchPoint = screenholdEvent.TouchPoint
    
        local str_var
        for i =1, #self.Index do
        	if self.Index[i] == touchId then
        		self.Entity.TransformComponent.WorldPosition = _UILogic:ScreenToWorldPosition(touchPoint):ToVector3()
        		break
        	end
        end
        end
    
        @ExecSpace("ClientOnly")
        method void AddHandler(integer touchId)
        	if self.Handler ~= nil then
        		return
        	end
    
        	table.insert(self.index, touchId)
        -- 	self.Handler = _InputService:ConnectEvent(ScreenTouchHoldEvent, self.Move)  -- [Commented: MLUA requires handler pattern]
        end
    
        @EventSender("Self")
        handler HandleTouchEvent(TouchEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: TouchReceiveComponent
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local TouchId = event.TouchId
        local TouchPoint = event.TouchPoint
        ---------------------------------------------------------
    
        self:AddHandler(TouchId)
        end
    
        @EventSender("Self")
        handler HandleTouchHoldEvent(TouchHoldEvent event)
        --------------- Native Event Sender Info ----------------
        -- Sender: TouchReceiveComponent
        -- Space: Client
        ---------------------------------------------------------
    
        -- Parameters
        local TouchId = event.TouchId
        local TouchPoint = event.TouchPoint
        ---------------------------------------------------------
        self:AddHandler(TouchId)
        end
    
        @EventSender("Self")
        handler HandleTouchReleaseEvent(TouchReleaseEvent event)
        	--------------- Native Event Sender Info ----------------
        	-- Sender: TouchReceiveComponent
        	-- Space: Client
        	---------------------------------------------------------
    
        	-- Parameters
        	local TouchId = event.TouchId
        	local TouchPoint = event.TouchPoint
        	---------------------------------------------------------
        	for i =1, #self.Index do
        		if self.Index[i] == TouchId then
        			table.remove(self.index, i)
        			if #self.Index <= 0 then
        -- 				_InputService:DisconnectEvent(ScreenTouchHoldEvent, self.eventHandler)  -- [Commented: Use handler pattern instead]
        				self.Handler = nil
        			end
        			break
        		end
        	end
        end
    end
    

# SeeAlso

  * TransformComponent
  * TouchEvent
  * UILogic
  * EntityService
  * InputService