VCIを出した際に、message(”SakuraTransformPosPlz”)にて、VCIの個別IDが共有されるので、それを使って、message(”SakuraTransformSend”)にてTransformの情報を送ると、「桜の木」を個別に移動させることができます。ただし、messageでの送信量の関係から、10本毎での移動の指示となります。分割用ID:1が1~10番目の桜の木、ID:2が11~20番目、ID:3が21番目のみの「桜の木」の移動の指示となっています。また、桜の枝を非アクティブにすることにより、「桜の木」を固定して設置することも可能です。
--桜の木のTransform用メッセージ
--message[1]:VCIの個別ID、固定できないIDのため、読み込み時のみIDを共有
--message[2]:Transform用Tbl
--message[3]:メッセージ容量が足りないため、分割用のID数値(1~3)
--message[4]:コントローラーのON/OFF、OFF(非アクティブ)で疑似的な固定
function sakuraTransform(sender, name, message)
if message[1] == vci.assets.GetInstanceId() then
for i = 1, 10 do
--桜の木は21個なので、それ以降は処理しない
if message[3] == 3 and i == 2 then
break
end
if message[2][i]["Act"] ~= nil then
if message[2][i]["Act"] then
local tempPos = Vector3.zero
if message[2][i]["Act"] then
tempPos.x = message[2][i]["Pos"]["x"]
tempPos.y = message[2][i]["Pos"]["y"]
tempPos.z = message[2][i]["Pos"]["z"]
end
local tempRot = Quaternion.identity
if message[2][i]["Act"] then
tempRot.x = message[2][i]["Rot"]["x"]
tempRot.y = message[2][i]["Rot"]["y"]
tempRot.z = message[2][i]["Rot"]["z"]
tempRot.w = message[2][i]["Rot"]["w"]
end
local tempScl = Vector3.one
if message[2][i]["Act"] then
tempScl.x = message[2][i]["Scl"]["x"]
tempScl.y = message[2][i]["Scl"]["y"]
tempScl.z = message[2][i]["Scl"]["z"]
end
vci.assets.GetTransform("CherryTree"..i+10*(message[3]-1)).SetPosition(tempPos)
vci.assets.GetTransform("CherryTree"..i+10*(message[3]-1)).SetRotation(tempRot)
vci.assets.GetTransform("CherryTree"..i+10*(message[3]-1)).SetLocalScale(tempScl)
sakuraEffect[i+10*(message[3]-1)].Stop()
sakuraEffect[i+10*(message[3]-1)].Play()
else
sakuraEffect[i+10*(message[3]-1)].Stop()
end
vci.assets.GetTransform("CherryTree"..i+10*(message[3]-1)).SetActive(message[2][i]["Act"])
transTbl[i+10*(message[3]-1)]["Act"] = message[2][i]["Act"]
end
end
vci.state.Set("SakuraTrans", transTbl)
if sakuraStick.IsMine then
if message[4] then
sakuraStick._ALL_SetActive(false)
vci.state.Set("FixState", true)
else
sakuraStick._ALL_SetActive(true)
vci.state.Set("FixState", false)
end
end
end
end
vci.message.On("SakuraTransformSend", sakuraTransform)
function updateAll()
if not(initialFlag) then
--初期ロード時のstate初期化、VCIのID発行、桜の木の非アクティブ
--それ以外は、アクティブ・非アクティブの同期
--コントローラーのアクティブ・非アクティブの同期
if vci.state.Get("SakuraTrans") == nil then
if vci.assets.IsMine then
vci.state.Set("SakuraTrans", transTbl)
vci.message.Emit("SakuraTransformPosPlz", vci.assets.GetInstanceId())
end
for i = 1, 21 do
sakura[i].SetActive(false)
end
else
for i = 1, 21 do
sakura[i].SetActive(vci.state.Get("SakuraTrans")[i]["Act"])
if vci.state.Get("SakuraTrans")[i]["Act"] then
sakuraEffect[i].Play()
else
sakuraEffect[i].Stop()
end
end
end
if vci.state.Get("FixState") ~= nil then
sakuraStick.SetActive(not(vci.state.Get("FixState")))
end
for i = 1, 21 do
sakuraBtn[i].SetActive(false)
end
initialFlag = true
end
end
移動させるためのスクリプトの一例は以下の通りです。
function sakuraTransformMessage(sender, name, message)
for j = 1, 3 do
local tempTbl = {}
for i = 1, 10 do
tempTbl[i] = {}
if vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)) ~= nil then
tempTbl[i]["Act"] = true
else
tempTbl[i]["Act"] = nil
end
if tempTbl[i]["Act"] then
tempTbl[i]["Pos"] = {
x = vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)).GetPosition().x,
y = vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)).GetPosition().y,
z = vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)).GetPosition().z
}
local rot = vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)).GetRotation()
tempTbl[i]["Rot"] = {
x = (rot*Quaternion.euler(90, 0,0)).x,
y = (rot*Quaternion.euler(90, 0,0)).y,
z = (rot*Quaternion.euler(90, 0,0)).z,
w = (rot*Quaternion.euler(90, 0,0)).w
}
tempTbl[i]["Scl"] = {
x = vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)).GetLocalScale().x*0.85,
y = vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)).GetLocalScale().y*0.85,
z = vci.assets.GetTransform("CTPos"..i+10*(j-1)+21*(sakuraSetCnt-1)).GetLocalScale().z*0.85
}
end
end
vci.message.Emit("SakuraTransformSend", {message, tempTbl, j, true})
end
end
vci.message.on("SakuraTransformPosPlz", sakuraTransformMessage)