// 给人物换装 public void ChangePart(string part, string model, EquipModelViewRecord record, bool sync) { SetPartModel(part, model); // 如果还没有加载完基础模型,则等待 if (_body == null || _loading > 0) { _avatarLoadQueue.Enqueue(new KeyValuePair <string, WaitingResource>(part, new WaitingResource { ModelPath = model, ModelViewRecord = record })); return; } //AvatarData adata = DataMgr.Instance.GetAvatarData(avatarId); _loading++; ComplexObjectPool.NewObject(model, obj => { _loading--; if (GetPartModel(part) != model) { ComplexObjectPool.Release(obj); TryContinue(); return; } if (null != obj) { ChangePart(obj, part, record, sync); } TryContinue(); }, null, null, sync); }
public void MountWeapon(MountPoint p, string model, WeaponMountRecord mountRecord, EquipModelViewRecord record, int part, bool sync, Action <GameObject> callback = null) { mWeaponModels[part] = model; // remove all the objs with the same tag. List <GameObject> l; if (!_parts.TryGetValue(part, out l)) { l = new List <GameObject>(); _parts.Add(part, l); } var lCount0 = l.Count; for (var i = 0; i < lCount0; i++) { ComplexObjectPool.Release(l[i], Layer == UnityEngine.LayerMask.NameToLayer("UI")); } l.Clear(); if (string.IsNullOrEmpty(model)) { TryContinue(); return; } if (_body == null || _loading > 0) { _avatarLoadQueue.Enqueue(new KeyValuePair <string, WaitingResource>(p.ToString(), new WaitingResource { ModelPath = model, ModelViewRecord = record, IsWeapon = true, Part = part, Callback = callback, WeaponMountRecord = mountRecord })); return; } _loading++; ComplexObjectPool.NewObject(model, obj => { _loading--; string value; if (mWeaponModels.TryGetValue(part, out value) && value != model) { ComplexObjectPool.Release(obj, Layer == UnityEngine.LayerMask.NameToLayer("UI")); TryContinue(); return; } if (null != obj) { if (!MountWeapon(p, obj, mountRecord, record, part, sync, callback)) { ComplexObjectPool.Release(obj); TryContinue(); return; } } if (record == null) { TryContinue(); return; } var recordEffectPathLength1 = record.EffectPath.Length; for (var i = 0; i < recordEffectPathLength1; i++) { if (!string.IsNullOrEmpty(record.EffectPath[i])) { _loading++; var index = i; ComplexObjectPool.NewObject(record.EffectPath[index], effect => { _loading--; string _model; if (mWeaponModels.TryGetValue(part, out _model) && _model != model) { ComplexObjectPool.Release(effect, Layer == UnityEngine.LayerMask.NameToLayer("UI")); TryContinue(); return; } if (obj != null && effect != null) { if (record.EffectMount[index] == -1) { AddEffect(obj.transform, effect, new Vector3(record.EffectPosX[index], record.EffectPosY[index], record.EffectPosZ[index]), Quaternion.Euler(record.EffectDirX[index], record.EffectDirY[index], record.EffectDirZ[index]), part); } else { AddEffect( GetPart(_body.transform, ((MountPoint)record.EffectMount[index]).ToString()), effect, new Vector3(record.EffectPosX[index], record.EffectPosY[index], record.EffectPosZ[index]), Quaternion.Euler(record.EffectDirX[index], record.EffectDirY[index], record.EffectDirZ[index]), part); } } TryContinue(); }, null, null, sync); } } TryContinue(); }, null, null, sync); }
private bool MountWeapon(MountPoint p, GameObject obj, WeaponMountRecord mountRecord, EquipModelViewRecord record, int part, bool sync, Action <GameObject> callback) { if (_body == null) { return(false); } if (obj == null) { return(false); } var parent = GetPart(_body.transform, p.ToString()); if (parent == null) { return(false); } var o = obj; var objTransform = o.transform; objTransform.parent = parent; objTransform.localPosition = new Vector3(mountRecord.PosX, mountRecord.PosY, mountRecord.PosZ); objTransform.localRotation = Quaternion.Euler(mountRecord.DirX, mountRecord.DirY, mountRecord.DirZ); objTransform.localScale = Vector3.one; o.SetLayerRecursive(Layer, LayerMask); o.SetRenderQueue(mRenderQueue); _parts[part].Add(o); if (callback != null) { callback(o); } Material material; Renderer renderer = o.renderer; if (renderer != null) { material = renderer.sharedMaterial; } else { renderer = objTransform.GetComponentInChildren <SkinnedMeshRenderer>(); if (renderer == null) { Logger.Log2Bugly("MountWeapon renderer is null, {0}, {1}, {2}", p, obj.name, record.Id); return(false); } material = renderer.sharedMaterial; } if (material == null) { return(false); } // 翅膀没有这个 if (!material.HasProperty("_BColor")) { return(true); } const string MainTexVariableName = "_MainTex"; if (IsMainPlayer) { _loading++; ResourceManager.PrepareResource <Material>(Resource.Material.MainPlayerMaterial, mat => { _loading--; if (!mat) { TryContinue(); return; } if (!o || !renderer) { TryContinue(); return; } if (!material) { TryContinue(); return; } var newMat = new Material(mat); newMat.SetTexture(MainTexVariableName, material.GetTexture(MainTexVariableName)); renderer.material = newMat; if (record != null) { newMat.SetColor("_BColor", new Color(record.FlowRed / 255.0f, record.FlowGreen / 255.0f, record.FlowBlue / 255.0f, record.FlowAlpha / 255.0f)); newMat.SetColor("_TexColor", new Color(record.SepcularRed / 255.0f, record.SepcularGreen / 255.0f, record.SepcularBlue / 255.0f, record.SepcularAlpha / 255.0f)); } else { newMat.SetColor("_BColor", Color.black); newMat.SetColor("_TexColor", Color.black); } ResourceManager.ChangeShader(objTransform); TryContinue(); }, true, true, sync); } else { if (renderer) { var newMat = new Material(material); newMat.SetTexture(MainTexVariableName, material.GetTexture(MainTexVariableName)); renderer.material = newMat; if (record != null) { newMat.SetColor("_BColor", new Color(record.FlowRed / 255.0f, record.FlowGreen / 255.0f, record.FlowBlue / 255.0f, record.FlowAlpha / 255.0f)); newMat.SetColor("_TexColor", new Color(record.SepcularRed / 255.0f, record.SepcularGreen / 255.0f, record.SepcularBlue / 255.0f, record.SepcularAlpha / 255.0f)); } else { newMat.SetColor("_BColor", Color.black); newMat.SetColor("_TexColor", Color.black); } ResourceManager.ChangeShader(objTransform); } } return(true); }
// 替换部件 private void ChangePart(GameObject avatarModel, string partName, EquipModelViewRecord record, bool sync) { // 先卸载当前部件 AvatarInfo currentInfo; if (_avatarInfo.TryGetValue(partName, out currentInfo)) { if (currentInfo.avatarPart != null) { Destroy(currentInfo.avatarPart); currentInfo.avatarPart = null; } if (currentInfo.defaultPart != null) { currentInfo.defaultPart.SetActive(true); } } // avatarModel是一个resource,并没有实例化 if (avatarModel == null) { return; } if (_body == null) { ComplexObjectPool.Release(avatarModel); return; } // 需要替换的部件 var avatarPart = GetPart(avatarModel.transform, partName); if (avatarPart == null) { ComplexObjectPool.Release(avatarModel); Logger.Error("{0} should contain a node name {1}", avatarModel.transform.name, partName); return; } // 将原始部件隐藏 var bodyPart = GetPart(_body.transform, partName); if (bodyPart != null) { bodyPart.gameObject.SetActive(false); } // 设置到body上的新物件 var newPart = new GameObject(partName); var partTransform = newPart.transform; partTransform.parent = _body.transform; partTransform.localPosition = Vector3.zero; partTransform.localRotation = Quaternion.Euler(0, 0, 0); var newPartRender = newPart.AddComponent <SkinnedMeshRenderer>(); var avatarRender = avatarPart.GetComponent <SkinnedMeshRenderer>(); { // foreach(var item in avatarPart.transform) var __enumerator6 = (avatarPart.transform).GetEnumerator(); while (__enumerator6.MoveNext()) { var item = (Transform)__enumerator6.Current; { var child = Instantiate(item.gameObject) as GameObject; if (null == child) { continue; } var childTransform = child.transform; childTransform.parent = partTransform; childTransform.localPosition = item.localPosition; childTransform.localScale = item.localScale; childTransform.localRotation = item.localRotation; } } } newPart.SetLayerRecursive(Layer, LayerMask); newPart.SetRenderQueue(mRenderQueue); // 刷新骨骼模型数据 SetBones(newPart, avatarPart.gameObject, _body); newPartRender.sharedMesh = avatarRender.sharedMesh; newPartRender.sharedMaterials = avatarRender.sharedMaterials; ComplexObjectPool.Release(avatarModel); const string MainTexVariableName = "_MainTex"; if (IsMainPlayer) { _loading++; ResourceManager.PrepareResource <Material>(Resource.Material.MainPlayerMaterial, mat => { _loading--; var newMat = new Material(mat); if (!newPartRender) { return; } newMat.SetTexture(MainTexVariableName, newPartRender.sharedMaterial.GetTexture(MainTexVariableName)); newPartRender.material = newMat; if (record != null) { newMat.SetColor("_BColor", new Color(record.FlowRed / 255.0f, record.FlowGreen / 255.0f, record.FlowBlue / 255.0f, record.FlowAlpha / 255.0f)); newMat.SetColor("_TexColor", new Color(record.SepcularRed / 255.0f, record.SepcularGreen / 255.0f, record.SepcularBlue / 255.0f, record.SepcularAlpha / 255.0f)); } else { newMat.SetColor("_BColor", Color.black); newMat.SetColor("_TexColor", Color.black); } ResourceManager.ChangeShader(partTransform); TryContinue(); }, true, true, sync); } else { var newMat = new Material(newPartRender.sharedMaterial); if (!newPartRender) { return; } newMat.SetTexture(MainTexVariableName, newPartRender.sharedMaterial.GetTexture(MainTexVariableName)); newPartRender.material = newMat; if (record != null) { newMat.SetColor("_BColor", new Color(record.FlowRed / 255.0f, record.FlowGreen / 255.0f, record.FlowBlue / 255.0f, record.FlowAlpha / 255.0f)); newMat.SetColor("_TexColor", new Color(record.SepcularRed / 255.0f, record.SepcularGreen / 255.0f, record.SepcularBlue / 255.0f, record.SepcularAlpha / 255.0f)); } else { newMat.SetColor("_BColor", Color.black); newMat.SetColor("_TexColor", Color.black); } ResourceManager.ChangeShader(partTransform); } // 记录换装信息 var info = new AvatarInfo(); info.partName = partName; if (bodyPart != null) { info.defaultPart = bodyPart.gameObject; } else { info.defaultPart = null; } info.avatarPart = newPart; _avatarInfo[partName] = info; // 检查Body的Animation的CullType, 否则会出现当所有装备都卸下时,动画没有了的问题 _body.animation.cullingType = AnimationCullingType.BasedOnUserBounds; _body.animation.localBounds = new Bounds(new Vector3(0, 0, 0), new Vector3(1, 2, 1)); }