Unity 연동 기초
Viven Script (Lua) 안에서 GameObject, Transform, Component 같은 Unity의 핵심 기능을 제어하는 방법을 소개합니다.
Viven Script 는 Lua 엔진에서 동작하지만, VIVEN Runtime 을 통해 Unity의 거의 모든 기능에 접근할 수 있습니다. 이 문서에서는 가장 많이 쓰이는 GameObject · Transform · Component 제어를 다룹니다.
GameObject
self.gameObject
스크립트가 부착된 GameObject는 self.gameObject 로 접근합니다.
SetActive — 활성화/비활성화
-- 본 스크립트가 부착된 GameObject를 활성화
self.gameObject:SetActive(true)
-- 다른 GameObject를 이름으로 찾아 비활성화
local target = GameObject.Find("Some Game Object")
target:SetActive(false)
Instantiate — 오브젝트 생성
Unity의 GameObject.Instantiate 를 그대로 호출해 Prefab 을 복제할 수 있습니다.
local go = GameObject.Instantiate(prefabReference)
go.transform.position = Vector(1, 1, 1)
VObject 가 포함된 Prefab을 Instantiate 로 런타임에 생성하는 것은 현재 지원되지
않습니다. 네트워크 동기화가 필요한 경우에는 VMap 빌드 시점에 미리 배치된
오브젝트만 사용하세요. 자세한 내용은 Network 개요 를 참고하세요.
Transform
스크립트가 붙은 GameObject의 Transform 에도 self.transform 으로 바로 접근합니다.
local t = self.transform
-- Y축 방향으로 1m 이동
t:Translate(Vector(0, 1, 0))
-- 위치·회전·스케일 직접 지정
t.position = Vector(0, 2, 0)
t.localScale = Vector(1, 1, 1)
Component
GetComponent
-- Type 으로 접근
local rigidBody = self.gameObject:GetComponent(typeof(RigidBody))
-- 문자열 이름으로도 접근 가능
local rb = self.gameObject:GetComponent("RigidBody")
rb:AddForce(Vector(0, 1, 0)) -- Y축으로 1의 힘 적용
AddComponent
-- 실행 중에 새 컴포넌트 부착
local rb = self.gameObject:AddComponent(typeof(RigidBody))
-- 문자열 이름도 가능
local rb2 = self.gameObject:AddComponent("RigidBody")
GetLuaComponent
다른 GameObject 에 부착된 Viven Lua Behaviour 에 접근할 때 사용합니다.
OnTriggerEnter 이벤트에서 상대 오브젝트의 스크립트 인스턴스를 얻는 전형적인 패턴입니다.
function onTriggerEnter(other)
-- Lua 컴포넌트 이름으로 가져오기
local lua = other.gameObject:GetLuaComponent("SomeLuaComponent")
if lua ~= nil then
lua:DoSomething()
end
end
자주 쓰이는 Vector 헬퍼
local up = Vector(0, 1, 0)
local right = Vector(1, 0, 0)
local zero = Vector(0, 0, 0)
Lua 인스펙터에서 변수를 public 하게 노출하려면 Injection 기능을 활용하세요.
인스펙터에서 GameObject, Component, 다른 Viven Script 참조를 주입받을 수 있어
GameObject.Find 없이도 깔끔하게 결합을 분리할 수 있습니다.
다음 단계
- Interaction 개요 — Grabbable · Sittable · Attach Point
- Network 개요 — VObject · View · RPC 동기화