这是通过读表的方式来读取模型从而实例化,加载实体
GF框架中给我们封装好了Entity的方法
我们这里需要调用即可
首先仿照上面的方法我们写一个创建人物的方法在这个脚本中
实体的具体信息在这个表中 AssetName是存的预制体的名字
还有每个角色所对应的具体信息同样存在表中
创建一个玩家角色的信息脚本
用于处理逻辑的类,目的是为了确定模型实体加载出来的位置,大小等信息
然后开始写创建脚本的逻辑,因为拿到了实体的数据,这个数据来自于PlayerRoleData这里可以F12进入导航
然后这里PlayerRoleData是由
CreateRoleForm中的
这里赋值的这个编号就叫做实体ID
CreateRoleForm脚本中 ----------- 让RoleID变成实体ID
这里写了一个方法可以在右边显示出来当前展示的角色和他的实体ID
CreateRoleForm还有一个方法就是随机名字
同样的还是读表,从DRFirstName表中读取名字
当我们每次要新建一个实体的时候这都是一个组
我们要更改他的组的数目
在这里我们新建一个PlayerRole组,这些参数都是框架做好的,分别是实例化间隔,实例化容量 实例化时间 还有优先级,
using GameFramework.DataTable;
using System;
using UnityEngine;
namespace StarForce
{
///
/// 角色的具体信息表
///
public class PlayerRoleData : EntityData
{
[SerializeField]
private string m_Name;
[SerializeField]
private string m_Describe;
[SerializeField]
private Vector3 m_PositionZ;
[SerializeField]
private Vector3 m_RotationY;
///
///
///
/// 实体id
/// 资源类型id
public PlayerRoleData(int entityId, int typeId)
: base(entityId, typeId)
{
IDataTable<DRPlayerRole> dtPlayerRoles = GameEntry.DataTable.GetDataTable<DRPlayerRole>();
DRPlayerRole drPlayerRole = dtPlayerRoles.GetDataRow(TypeId);
if (drPlayerRole == null)
{
return;
}
m_Name = drPlayerRole.Name;
m_Describe = drPlayerRole.Describe;
m_PositionZ = new Vector3(0, drPlayerRole.PositionY,0);
base.Position = m_PositionZ;
m_RotationY = new Vector3(0, drPlayerRole.RotationY, 0);
base.Rotation =Quaternion.Euler(m_RotationY);
}
}
}
using GameFramework;
using GameFramework.DataTable;
using System;
using UnityGameFramework.Runtime;
namespace StarForce
{
public static class EntityExtension
{
// 关于 EntityId 的约定:
// 0 为无效
// 正值用于和服务器通信的实体(如玩家角色、NPC、怪等,服务器只产生正值)
// 负值用于本地生成的临时实体(如特效、FakeObject等)
private static int s_SerialId = 0;
public static Entity GetGameEntity(this EntityComponent entityComponent, int entityId)
{
UnityGameFramework.Runtime.Entity entity = entityComponent.GetEntity(entityId);
if (entity == null)
{
return null;
}
return (Entity)entity.Logic;
}
public static void HideEntity(this EntityComponent entityComponent, Entity entity)
{
entityComponent.HideEntity(entity.Entity);
}
public static void HideEntityWithEntityId(this EntityComponent entityComponent,int entityTypeIndex)
{
//entityComponent.HideEntity(entity.Entity);
}
public static void AttachEntity(this EntityComponent entityComponent, Entity entity, int ownerId, string parentTransformPath = null, object userData = null)
{
entityComponent.AttachEntity(entity.Entity, ownerId, parentTransformPath, userData);
}
public static void ShowMyAircraft(this EntityComponent entityComponent, MyAircraftData data)
{
entityComponent.ShowEntity(typeof(MyAircraft), "Aircraft", data);
}
public static void ShowAircraft(this EntityComponent entityComponent, AircraftData data)
{
entityComponent.ShowEntity(typeof(Aircraft), "Aircraft", data);
}
public static void ShowThruster(this EntityComponent entityComponent, ThrusterData data)
{
entityComponent.ShowEntity(typeof(Thruster), "Thruster", data);
}
public static void ShowWeapon(this EntityComponent entityComponent, WeaponData data)
{
entityComponent.ShowEntity(typeof(Weapon), "Weapon", data);
}
public static void ShowArmor(this EntityComponent entityComponent, ArmorData data)
{
entityComponent.ShowEntity(typeof(Armor), "Armor", data);
}
public static void ShowBullet(this EntityComponent entityCompoennt, BulletData data)
{
entityCompoennt.ShowEntity(typeof(Bullet), "Bullet", data);
}
public static void ShowAsteroid(this EntityComponent entityCompoennt, AsteroidData data)
{
entityCompoennt.ShowEntity(typeof(Asteroid), "Asteroid", data);
}
///
///
///
///
/// 加载的实体具体信息数据
public static void ShowPlayerRole(this EntityComponent entityCompoennt, PlayerRoleData data)
{
entityCompoennt.ShowEntity(typeof(PlayerRoleEntity), "PlayerRole", data);
}
public static void ShowEffect(this EntityComponent entityComponent, EffectData data)
{
entityComponent.ShowEntity(typeof(Effect), "Effect", data);
}
private static void ShowEntity(this EntityComponent entityComponent, Type logicType, string entityGroup, EntityData data)
{
if (data == null)
{
Log.Warning("Data is invalid.");
return;
}
IDataTable<DREntity> dtEntity = GameEntry.DataTable.GetDataTable<DREntity>();
DREntity drEntity = dtEntity.GetDataRow(data.TypeId);
if (drEntity == null)
{
Log.Warning("Can not load entity id '{0}' from data table.", data.TypeId.ToString());
return;
}
entityComponent.ShowEntity(data.Id, logicType, AssetUtility.GetEntityAsset(drEntity.AssetName), entityGroup, data);
}
public static int GenerateSerialId(this EntityComponent entityComponent)
{
return --s_SerialId;
}
}
}
using StarForce;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace StarForce
{
///
/// 处理当前实体逻辑用的
///
public class PlayerRoleEntity : Entity
{
private PlayerRoleData mPlayerRoleData;
private int modelScele = 10;
protected internal override void OnShow(object userData)
{
base.OnShow(userData);
if (userData!=null)
{
mPlayerRoleData = (PlayerRoleData)userData;
}
///拿到实体数据 赋值给实例化出来的实体
transform.localPosition = mPlayerRoleData.Position;
transform.localRotation = mPlayerRoleData.Rotation;
transform.localScale = Vector3.one* modelScele;
}
protected internal override void OnHide(object userData)
{
base.OnHide(userData);
}
}
}
using System;
using GameFramework;
using GameFramework.DataTable;
using UnityEngine;
using UnityEngine.UI;
namespace StarForce
{
public class CreateRoleForm : UGuiForm
{
///
/// 随机名字的按钮
///
private Button randomANameBtn;
///
/// 输入名字的输入框
///
private InputField nameIpf;
///
/// 角色文本描述
///
private Text roleDes;
///
/// 当前展示的角色id
///
public int currentIndex = 80001;
protected internal override void OnInit(object userData)
{
base.OnInit(userData);
InitCom();
}
///
/// 界面的初始化
///
private void InitCom()
{
randomANameBtn = transform.Find("bottomPage/randomNameBtn").GetComponent<Button>() ;
if (randomANameBtn!=null)
{
randomANameBtn.onClick.AddListener(RandomAName);
}
nameIpf = transform.Find("bottomPage/nameInputField").GetComponent<InputField>() ;
roleDes = transform.Find("RegistPage/desText").GetComponent<Text>();
}
protected internal override void OnOpen(object userData)
{
base.OnOpen(userData);
///加载实体
GameEntry.Entity.ShowPlayerRole(new PlayerRoleData(currentIndex, currentIndex));
///获取对应实体的文本描述
IDataTable<DRPlayerRole> dtPlayerRoles = GameEntry.DataTable.GetDataTable<DRPlayerRole>();
DRPlayerRole dtPlayerRole = dtPlayerRoles.GetDataRow(currentIndex);
roleDes.text = " " + dtPlayerRole.Describe;
}
///
/// 切换角色
///
///
public void ShowPlayerRole(int roleIndex)
{
if (currentIndex != roleIndex)
{
GameEntry.Entity.HideEntity(currentIndex);
GameEntry.Entity.ShowPlayerRole(new PlayerRoleData(roleIndex, roleIndex));//要展示的实体
currentIndex = roleIndex;
///刷新觉得文本描述
IDataTable<DRPlayerRole> dtPlayerRoles = GameEntry.DataTable.GetDataTable<DRPlayerRole>();
DRPlayerRole dtPlayerRole = dtPlayerRoles.GetDataRow(currentIndex);
roleDes.text = " " + dtPlayerRole.Describe;
}
}
///
/// 随机一个名字
///
private void RandomAName()
{
nameIpf.text = string.Empty;
IDataTable<DRFirstName> dtNames = GameEntry.DataTable.GetDataTable<DRFirstName>();
int total = dtNames.Count;
int currentRandom =UnityEngine.Random.Range(1000,1000+total);
DRFirstName dtName= dtNames.GetDataRow(currentRandom);
nameIpf.text = dtName.FirstName + dtName.MiddleName + dtName.LastName;
}
}
}