Files
Fishing2/Assets/Scripts/Utils/PrefabCreator.cs
2025-05-10 12:49:47 +08:00

184 lines
6.0 KiB
C#

using UnityEngine;
namespace NBF
{
public static class PrefabCreator
{
#region
public static ReelAsset Create(this GameReels self, Transform parent, bool isPreview = false)
{
ReelAsset ret = null;
var prefab = self.Instantiate(parent);
prefab.transform.localPosition = Vector3.zero;
prefab.transform.localRotation = Quaternion.identity;
if (prefab.TryGetComponent<ReelAsset>(out var reel))
{
ret = reel;
if (!isPreview)
{
prefab.AddComponent<FReel>();
// prefab.AddJoint();
// var dis = prefab.AddComponent<FWaterDisplacement>();
// dis.moveCharacteristic = FWaterDisplacement.MoveCharacteristic.Custom;
// dis.objectDisplacement = 2;
// dis.outwaterMass = 0.1f;
}
}
return ret;
}
#endregion
#region
public static BobberAsset Create(this GameFloats self, Transform parent, bool isPreview = false)
{
BobberAsset ret = null;
var prefab = self.Instantiate(parent);
prefab.transform.localPosition = Vector3.zero;
prefab.transform.localRotation = Quaternion.identity;
if (prefab.TryGetComponent<BobberAsset>(out var lure))
{
ret = lure;
if (!isPreview)
{
prefab.AddComponent<FFloat>();
prefab.AddJoint();
var dis = prefab.AddComponent<FWaterDisplacement>();
dis.moveCharacteristic = FWaterDisplacement.MoveCharacteristic.Custom;
dis.objectDisplacement = 2;
dis.outwaterMass = 0.1f;
}
}
return ret;
}
#endregion
#region
public static LureAsset Create(this GameLures self, Transform parent, bool isPreview = false)
{
LureAsset ret = null;
var prefab = self.Instantiate(parent);
if (prefab.TryGetComponent<LureAsset>(out var lure))
{
ret = lure;
if (!isPreview)
{
prefab.AddComponent<FLure>();
prefab.AddJoint();
var rigidbody = prefab.GetComponent<Rigidbody>();
rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
rigidbody.linearDamping = 0.5f;
var dis = prefab.AddComponent<FWaterDisplacement>();
// dis.moveCharacteristic = FWaterDisplacement.MoveCharacteristic.Custom;
// dis.objectDisplacement = 2;
dis.outwaterMass = 0.5f;
}
if (lure.hookPoints != null && lure.hookPoints.Length > 0)
{
for (int i = 0; i < lure.hookPoints.Length; i++)
{
var hookPoint = lure.hookPoints[i];
var hook = self.hook[0];
var hookConfig = GameHooks.Get(hook);
var hookAsset = hookConfig.Create(hookPoint, true);
hookAsset.transform.localPosition = Vector3.zero;
hookAsset.transform.localRotation = Quaternion.identity;
var configurableJoint = hookAsset.GetComponent<ConfigurableJoint>();
configurableJoint.connectedBody = prefab.GetComponent<Rigidbody>();
}
}
}
return ret;
}
public static BaitAsset Create(this GameBaits self, Transform parent, bool isPreview = false)
{
var prefab = self.Instantiate(parent);
BaitAsset ret = null;
if (prefab.TryGetComponent<BaitAsset>(out var hook))
{
ret = hook;
}
prefab.transform.localPosition = Vector3.zero;
prefab.transform.localRotation = Quaternion.identity;
if (!isPreview)
{
prefab.AddComponent<FBait>();
}
return ret;
}
#endregion
#region
public static HookAsset Create(this GameHooks self, Transform parent, bool isPreview = false)
{
var prefab = self.Instantiate(parent);
HookAsset ret = null;
if (prefab.TryGetComponent<HookAsset>(out var hook))
{
ret = hook;
}
prefab.transform.localPosition = Vector3.zero;
prefab.transform.localRotation = Quaternion.identity;
if (!isPreview)
{
prefab.AddComponent<FHook>();
var d = prefab.AddComponent<FWaterDisplacement>();
d.outwaterMass = 0.1f;
}
return ret;
}
#endregion
#region
public static void AddJoint(this GameObject prefab)
{
var rigidbody = prefab.GetComponent<Rigidbody>();
if (rigidbody == null)
{
rigidbody = prefab.AddComponent<Rigidbody>();
}
var configurableJoint = prefab.GetComponent<ConfigurableJoint>();
if (configurableJoint == null)
{
configurableJoint = prefab.AddComponent<ConfigurableJoint>();
}
configurableJoint.xMotion = ConfigurableJointMotion.Locked;
configurableJoint.yMotion = ConfigurableJointMotion.Locked;
configurableJoint.zMotion = ConfigurableJointMotion.Locked;
// configurableJoint.angularXMotion = ConfigurableJointMotion.Limited;
// configurableJoint.angularYMotion = ConfigurableJointMotion.Limited;
// configurableJoint.angularZMotion = ConfigurableJointMotion.Limited;
}
#endregion
}
}