Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ダイナミックボーン一括設定ユーティリティー
/*
ダイナミックボーンの設定を一括で同じパラメーターに設定するツール
Copyright (c) [2020] [gamebox]
ver1.3:2020/8/29 コンパイルエラー修正。URL修正
ver1.1:2020/1/30
・説明ページ
 http://www.unitygamebox.com/?p=14
・ライセンス:ご自由に使用/改変/再配布 OK
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class DynamicBoneUtilBatchSetting : MonoBehaviour
{
[Header("【設定するボーンリスト】")]
public Transform[] mBones = null;
[Header("【更新レート】")]
public float mUpdateRate = 60;
[Header("【更新モード】")]
public DynamicBone.UpdateMode mUpdateMode = DynamicBone.UpdateMode.Normal;
[Range(0f, 1f)]
[Header("【減衰力】")]
[Tooltip("減衰力の設定\n揺れを抑制する力 \n大:ゆっくり大振り 小:素早く小刻み")]
public float mDamping;
[Header("減衰力のカーブ")]
public AnimationCurve mDanpingDistrib;
[Range(0f, 1f)]
[Header("【弾力】")]
[Tooltip("弾力の設定\n元に形状に戻ろうとする力を調整 \n大:ゆっくり大振り 小:素早く小刻み")]
public float mElasticity;
[Header("弾力のカーブ")]
public AnimationCurve mElasticityDistrib;
[Range(0f, 1f)]
[Header("【剛性】")]
[Tooltip("揺れ幅の大きさを調整 \n大:揺れ幅が小さくなる 小:揺れ幅が大きくなる")]
public float mStiffness;
[Header("剛性のカーブ")]
public AnimationCurve mStiffnessDistrib;
[Range(0f, 1f)]
[Header("【慣性】")]
[Tooltip("揺らすのに必要な位置変化(移動量/速度)の大きさを調整 \n大:揺らすには大きな位置変化が必要 小:小さい位置変化で揺らせる")]
public float mInert;
[Header("慣性のカーブ")]
public AnimationCurve mInertDistrib;
[Range(0f, 1f)]
[Header("【摩擦係数】")]
[Tooltip("摩擦というよりはコリジョンが触れた時の弾き方のパラメーター")]
public float mFriction;
[Header("摩擦のカーブ")]
public AnimationCurve mFrictionDistrib;
[Header("【コリジョン用半径】")]
public float mRadius;
[Header("コリジョン半径のカーブ")]
public AnimationCurve mRadiusDistrib;
[Header("ボーンの長さOffset")]
[Tooltip("EndLengthがゼロでない場合、最後に追加のボーンが生成されます")]
public float mEndLength = 0f;
[Header("ボーンの長さOffset")]
[Tooltip("End Offsetがゼロでない場合、最後に追加のボーンが生成されます")]
public Vector3 mEndoffset = Vector3.zero;
[Header("重力")]
public Vector3 mGravity = Vector3.zero;
[Header("力を付与")]
[Tooltip("常に設定した数値分を付与する")]
public Vector3 mForce = Vector3.zero;
[Header("コライダ設定を反映させる")]
public bool mUseColliderSettings = true;
[Header("【コライダリスト】")]
public List<DynamicBoneColliderBase> mColliders;
[Header("除外設定を反映させる")]
public bool mUseExclusionsSettings = true;
[Header("【除外リスト】")]
public List<Transform> mExclusions;
[Header("【軸固定】")]
public DynamicBone.FreezeAxis m_FreezeAxis = DynamicBone.FreezeAxis.None;
[Header("距離によって効果をOFFにする")]
public bool mDistantDisable = false;
[Header("OFFにする為の参照オブジェクト")]
public Transform mReferenceObject = null;
[Header("OFFにする距離")]
public float mDistanceToObject = 20f;
[System.NonSerialized]
public DynamicBoneUtilBatchSetting mDynamicBoneUtilBatchSetting;
// Use this for initialization
void Reset()
{
mDynamicBoneUtilBatchSetting = GetComponent<DynamicBoneUtilBatchSetting>();
var db = GetComponent<DynamicBone>();
if (!db) { return; }
mUpdateRate = db.m_UpdateRate;
mUpdateMode = db.m_UpdateMode;
mDamping = db.m_Damping;
mElasticity = db.m_Elasticity;
mStiffness = db.m_Stiffness;
mInert = db.m_Inert;
mFriction = db.m_Friction;
mFrictionDistrib = db.m_FrictionDistrib;
mRadius = db.m_Radius;
mDanpingDistrib = db.m_DampingDistrib;
mElasticityDistrib = db.m_ElasticityDistrib;
mStiffnessDistrib = db.m_StiffnessDistrib;
mInertDistrib = db.m_InertDistrib;
mRadiusDistrib = db.m_RadiusDistrib;
mEndLength = db.m_EndLength;
mEndoffset = db.m_EndOffset;
mGravity = db.m_Gravity;
mForce = db.m_Force;
if (mUseColliderSettings) { mColliders = db.m_Colliders; }
if (mUseExclusionsSettings) { mExclusions = db.m_Exclusions; }
mDistantDisable = false;
mReferenceObject = db.m_ReferenceObject;
mDistanceToObject = db.m_DistanceToObject;
}
/// <summary>
/// インスペクタの値が変更された時に呼ばれる
/// </summary>
void OnValidate()
{
if (mBones == null) { return; }
if (mBones.Length == 0) { return; }
var dbs = GetComponents<DynamicBone>();
// アタッチされていないボーンがあれば追加
var attachedBones = dbs.Select(db => db.m_Root);
var unAttachedBones = mBones.Except(attachedBones).ToArray();
foreach (var bone in unAttachedBones)
{
if (bone == null) break;
var db = gameObject.AddComponent<DynamicBone>();
db.m_Root = bone;
db.m_DistantDisable = true;
}
// 追加した場合コンポーネントをリロード
if (unAttachedBones.Length > 0)
{
dbs = GetComponents<DynamicBone>();
}
foreach (var dynamicBone in dbs)
{
dynamicBone.m_UpdateRate = mUpdateRate;
dynamicBone.m_UpdateMode = mUpdateMode;
dynamicBone.m_Damping = mDamping;
dynamicBone.m_DampingDistrib = mDanpingDistrib;
dynamicBone.m_Elasticity = mElasticity;
dynamicBone.m_ElasticityDistrib = mElasticityDistrib;
dynamicBone.m_Stiffness = mStiffness;
dynamicBone.m_StiffnessDistrib = mStiffnessDistrib;
dynamicBone.m_Inert = mInert;
dynamicBone.m_InertDistrib = mInertDistrib;
dynamicBone.m_Friction = mFriction;
dynamicBone.m_FrictionDistrib = mFrictionDistrib;
dynamicBone.m_Radius = mRadius;
dynamicBone.m_RadiusDistrib = mRadiusDistrib;
dynamicBone.m_EndLength = mEndLength;
dynamicBone.m_EndOffset = mEndoffset;
dynamicBone.m_Gravity = mGravity;
dynamicBone.m_Force = mForce;
if (mUseColliderSettings) { dynamicBone.m_Colliders = mColliders; }
if (mUseExclusionsSettings) { dynamicBone.m_Exclusions = mExclusions; }
dynamicBone.m_DistantDisable = mDistantDisable;
dynamicBone.m_ReferenceObject = mReferenceObject;
dynamicBone.m_DistanceToObject = mDistanceToObject;
dynamicBone.UpdateParameters();
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(DynamicBoneUtilBatchSetting))]
public class DynamicBoneUtilBatchSettingInspector : Editor
{
public override void OnInspectorGUI()
{
DynamicBoneUtilBatchSetting ubs = target as DynamicBoneUtilBatchSetting;
EditorGUILayout.HelpBox("version 1.3:2020/8/29:©gamebox\nダイナミックボーン一括設定ツール:複数のボーンに同じパラメーターを設定できます\nDynamicBone Version1.2.1対応", MessageType.None);
GUI.color = Color.green;
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("使い方説明ページ"))
{
Application.OpenURL("http://www.unitygamebox.com/?p=14");
}
EditorGUILayout.EndHorizontal();
GUI.color = new Color32(230, 200, 200, 255);
EditorGUILayout.HelpBox("実行中に変更した値は終了後に消えますが、\n実行中にcopyボタンを押して、終了後にpasteボタンを押すと実行時の値に戻す事ができます", MessageType.Info);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("設定をcopy"))
{
//var comp = ubs.get;
UnityEditorInternal.ComponentUtility.CopyComponent( ubs );
}
if (GUILayout.Button("設定をpaste"))
{
UnityEditorInternal.ComponentUtility.PasteComponentValues( ubs );
}
EditorGUILayout.EndHorizontal();
GUI.color = Color.yellow;
base.OnInspectorGUI();
GUI.color = Color.white;
}
}
#endif
}
@gamebox777

This comment has been minimized.

Copy link
Owner Author

@gamebox777 gamebox777 commented Jan 29, 2020

@gamebox777

This comment has been minimized.

Copy link
Owner Author

@gamebox777 gamebox777 commented Jan 29, 2020

2020/1/30 ver1.1 摩擦設定を忘れていたので修正

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.