实验用品播放器
<p>[TOC]</p>
<table>
<thead>
<tr>
<th>作者</th>
<th>刘备(594682)</th>
</tr>
</thead>
<tbody>
<tr>
<td>更新日期</td>
<td>2025-2-27</td>
</tr>
<tr>
<td>版本</td>
<td>V1.2.1</td>
</tr>
</tbody>
</table>
<h1>前言</h1>
<p><strong>新人、开发组件人员、使用编辑器者必读</strong>
<strong>该文档会随着项目的完善,持续完善修改</strong>
实验用品播放器是以插件的形式存在的,如要导入新工程需要导出插件包(还要引入unlua库),播放器工程在文档的[播放器工程](#播放器工程 "播放器工程")
【实验用品目录结构】链接:<a href="https://mubu.com/doc/6pLIF86NK2J#m">https://mubu.com/doc/6pLIF86NK2J#m</a>
【新实验用品数据】链接:<a href="https://www.mubu.com/doc/2deFBu522td#m">https://www.mubu.com/doc/2deFBu522td#m</a>
最新版本删除了LUA的实现</p>
<h1>1.快速创建</h1>
<p>> 可以直接<strong>拷贝 TemplateElement以及相关数据和模型来创建器材</strong>组件、资源和数据调整成自己的内容即可。
> 例如:拷贝器材文件夹Content/Script/ElementResources/Elements/TemplateElement
> 命名为Content/Script/ElementResources/Elements/MyElement
> 然后重命名BP_TemplateElement为自己的名称
> 再打开蓝图,添加自己所需的组件、模型和数据。</p>
<h4>Lua静态绑定和配置关联</h4>
<h5>1 参数设置</h5>
<p>新的器材一定要设置器材EquipmentCode和相关MetaInfo数据
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=13c0cf7edf88f94362b083b739e71277&amp;file=file.png" alt="" />
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=d4c14840640e7f528382d037fcff381b&amp;file=file.png" alt="" /></p>
<h5>2 Lua绑定</h5>
<p>如果需要开启Lua功能,则从模板文件中拷贝Equipment.lua到自己的Lua目录。例如:
例如:MyEquipElement,则拷贝到 \Content\Script\ElementResources\Elements\MyEquipElement\Equipment.lua
然后通过蓝图启动
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=9911b9732386f2cf92653b25ff0437ad&amp;file=file.png" alt="" />
运行后看到:
<code>========Lua ReceiveBeginPlay succeeded!========</code>
则表示Lua初始化成功</p>
<h4>创建组件</h4>
<p>组件必须实现 IEquipmentComponent接口,代码可参考UTemplateEquipmentComponent.cpp。</p>
<h5>1.组件数据</h5>
<p>组件需要实现接口,定义自己的组件struct数据.
struct的定义<strong>支持基本类型和组件类型</strong>,并且可以在数据设定界面<strong>直接选取组件</strong>
参考代码如下:</p>
<pre><code class="language-cpp">USTRUCT(BlueprintType)
struct FTemplateEquipmentParams : public FComponentParams
{
GENERATED_BODY()
FTemplateEquipmentParams()
{
}
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int IntTest = 0;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool BoolTest = false;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString StringTest;
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = &quot;Equipment&quot;, meta=(UseComponentPicker))
FComponentReference MeshTest;
};</code></pre>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=480b8be920d98c545d04a14369221f26&amp;file=file.png" alt="" /></p>
<h5>2.组件获取</h5>
<p>参考代码如下:</p>
<pre><code class="language-cpp">auto Component = TableRow.MeshTest.GetComponent(GetOwner());</code></pre>
<h4>添加组件</h4>
<p>在器材蓝图添加自己开发的组件,例如:
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=3ede2c0f2b454f6c4af67b5aaae93747&amp;file=file.png" alt="" /></p>
<h4>数据绑定</h4>
<p>数据绑定用于同步Lua和CPP的状态和变量,当变量修改时,同步绑定的函数调用</p>
<h5>1 Lua端绑定和修改方法</h5>
<pre><code class="language-lua">-- Lua数据绑定回调
Binding.Bind(M.luaTb, &quot;State&quot;, function(new, old)
print(&quot;State = &quot; .. new .. &quot; old = &quot; .. old)
self.initTb.State = new
end)
Binding.Bind(M.luaTb, &quot;testStr&quot;, function(new, old)
print(&quot;testStr = &quot; .. new .. &quot; old = &quot; .. old)
self.initTb.testStr = new
end)
Binding.Bind(M.luaTb, &quot;testBool&quot;, function(new, old)
print(new)
self.initTb.testBool = new
end)
-- Lua数据修改
self.luaConfig:Broadcast(&quot;State&quot;,math.random(1000))
self.luaConfig:Broadcast(&quot;testStr&quot;,&quot;hello&quot;)
self.luaConfig:Broadcast(&quot;testBool&quot;,true)</code></pre>
<h5>2 CPP端绑定和修改方法</h5>
<pre><code class="language-cpp">// 绑定Cpp回调
Equipment-&gt;LuaCallback.AddDynamic(this, &amp;UTemplateEquipmentComponent::OnStateChange);
void UTemplateEquipmentComponent::OnStateChange(const FString&amp; Key,FEquipmentParams Value)
{
UE_LOG(LogTemp, Log, TEXT(&quot;%s value is %f&quot;), *Key, Value.floatValue);
}
// 数据同步
Equipment-&gt;Broadcast(StateName,100);
Equipment-&gt;Broadcast(StateName,&quot;Hello&quot;);
Equipment-&gt;Broadcast(StateName,true);</code></pre>
<h5>3 蓝图修改动态绑定数据</h5>
<p>当Lua中存在绑定数据时,以TemplateElement为例:</p>
<pre><code class="language-lua">M.initTb = {
State = 99,
testStr = &quot;hello&quot;,
testBool = false,
}</code></pre>
<p>蓝图实例会找到对应的绑定数据,自动解析并填充绑定数据
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=26348c37226d778259c73e0628c43087&amp;file=file.png" alt="" /></p>
<p><strong>然后你可以通过修改对应的参数值,来达到改变绑定数据所对应的函数。</strong></p>
<h4>创建药品</h4>
<p>药品容器本身就是器材,与器材不同之处是需要添加器材数据和化学容器组件</p>
<h5>1.创建药品Actor</h5>
<p>参考TemplateDrugElement</p>
<h5>2.设置药品数据</h5>
<p>设置药品配置名称
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=008a08ef581cdf3798caac6ac5a923e3&amp;file=file.png" alt="" />
> 药品数据暂时是放在Content\Script\ElementResources\DrugData目录,后续会根据颗粒下载获取。
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=1a72826d9871fdbc11012f081aa0ae43&amp;file=file.png" alt="" /></p>
<h4>创建物理约束和质量</h4>
<h5>1.创建物理约束</h5>
<p>实验用品如果有多个子对象时,需要根据网格组件区分为主物件和子物件,并将子物件放在主物件的节点下,以便开启物理约束。注意:网格节点不需要自己开启物理模拟,否则约束会失效。
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=1bb5f524dabcb820e88f551b8598af2f&amp;file=file.png" alt="" /></p>
<h5>2.添加质量</h5>
<p>实验用品需要根据实际的器材填写重量,例如:100kg的哑铃。
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=1a4e5c201a74ea1cb6cadf82e22f21aa&amp;file=file.png" alt="" /></p>
<h4>语言包</h4>
<h5>多语言文件</h5>
<p>器材文件夹下会有Language目录,用于存放语言包配置,例如:zh_CN.json
在使用多语言的地方直接填写Text的key即可,例如:在需要使用语言包的地方填入ObjTitle
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=df7c78c2e14d91c0c5a4ed2113b76703&amp;file=file.png" alt="" /></p>
<h1>播放器工程</h1>
<p>因为该工程用到了unlua,需要安装cmake才能正常编译
<a href="http://git.sdp.nd/app-code/equipment-player.git">http://git.sdp.nd/app-code/equipment-player.git</a></p>