我正在尝试用脚本生成字符。我在资源文件夹下有 ' playerObj
' 和 ' model
' pref。playerObj
具有所有与脚本相关的控制,和photonView
,,Camera
等,模型有animation
,,skin renderer
和photonView
。
我想要做的是,playerObj
一起实例化和实例化模型对象。对于模型 obj,我想添加一些皮肤网格,使其具有角色模型(我这样做是为了让玩家能够更改模型的装备),然后playerObj
与模型一起育儿。
问题是它正确地实例化了自己的角色,但实例化的其他角色是错误的。它将同时实例化playerObj
和model Obj
。但它不会渲染皮肤网格,也不会养育这两个……我不知道我做错了什么或遗漏了什么……
正如您在脚本中看到的那样,我正在使用三个不同的脚本来生成字符。
谢谢你。
播放器初始化.cs
public IEnumerator CreateCharacter()
{
var co = CurrentCharacter.GetByGUI(dbid);
co.InitModel(character, dbid);
m_loaded = true;
m_loading = false;
}
字符生成器.cs
public GameObject Generate(PhotonView pv)
{
int id1 = PhotonNetwork.AllocateViewID();
GameObject root = PhotonNetwork.Instantiate ("Model", new Vector3 (0, 0, 0), new Quaternion (0, 0, 0, 0),0);
root.name = "body";
Generate (pv, root);
return root;
}
public GameObject Generate(PhotonView pv, GameObject root)
{
int id1 = PhotonNetwork.AllocateViewID();
putSkin (root);
pv.RPC("putSkin", PhotonTargets.Others, root, id1, PhotonNetwork.player);
return root;
}
// Creates a character based on the currentConfiguration recycling a
// character base, this way the position and animation of the character
// are not changed.
[RPC]
public void putSkin(GameObject root){
float startTime = Time.realtimeSinceStartup;
// The SkinnedMeshRenderers that will make up a character will be
// combined into one SkinnedMeshRenderers using multiple materials.
// This will speed up rendering the resulting character.
List<CombineInstance> combineInstances = new List<CombineInstance>();
List<Material> materials = new List<Material>();
List<Transform> bones = new List<Transform>();
Transform[] transforms = root.GetComponentsInChildren<Transform>();
foreach (CharacterElement element in currentConfiguration.Values)
{
SkinnedMeshRenderer smr = element.GetSkinnedMeshRenderer();
materials.AddRange(smr.materials);
for (int sub = 0; sub < smr.sharedMesh.subMeshCount; sub++)
{
CombineInstance ci = new CombineInstance();
ci.mesh = smr.sharedMesh;
ci.subMeshIndex = sub;
combineInstances.Add(ci);
}
// As the SkinnedMeshRenders are stored in assetbundles that do not
// contain their bones (those are stored in the characterbase assetbundles)
// we need to collect references to the bones we are using
foreach (string bone in element.GetBoneNames())
{
foreach (Transform transform in transforms)
{
if (transform.name != bone) continue;
bones.Add(transform);
break;
}
}
Object.Destroy(smr.gameObject);
}
// Obtain and configure the SkinnedMeshRenderer attached to
// the character base.
SkinnedMeshRenderer r = root.GetComponent<SkinnedMeshRenderer>();
r.sharedMesh = new Mesh();
r.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false);
r.bones = bones.ToArray();
r.materials = materials.ToArray();
r.useLightProbes = true;
Debug.Log("Generating character took: "
+ (Time.realtimeSinceStartup - startTime) * 1000 + " ms");
}
当前字符.cs
[PunRPC]
public void InitModel(GameObject prefab, GameObject model, int id){
character = prefab;
InitModel (model, id);
}
public void InitModel(GameObject model, int id)
{
parenting (model, id);
_photonView.RPC ("parenting", PhotonTargets.Others, model, id);
}
[RPC]
void parenting(GameObject model, int id){
model.layer = LayerMask.NameToLayer ("Girl");
model.transform.parent = character.transform;
model.transform.localPosition = Vector3.zero;
InitMouseLook (model);
InitAnimations (model);
if (id == PersistentData.Instance.LoggedInUser.m_id) {
CurrentCharacter.persistentMainModel = model;
}
}