# 【Unity】モデルパーツの透明度について

**URL:** <https://creatorsforum.live2d.com/t/topic/528>\
**Category:** トラブル報告\
**Created:** [2022 年 2 月 2 日午後 3:47 UTC](https://creatorsforum.live2d.com/t/topic/528 "2022-02-02T15:47:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lexer\_Flanagan](https://sea1.discourse-cdn.com/flex015/user_avatar/creatorsforum.live2d.com/lexer_flanagan/32/275_2.png) [@Lexer\_Flanagan](https://creatorsforum.live2d.com/u/Lexer_Flanagan)\
**Post date:** [2022 年 2 月 2 日午後 3:47 UTC](https://creatorsforum.live2d.com/t/topic/528/1 "2022-02-02T15:47:57Z")

</div>

【Cubism SDKのバージョン】 Cubism 4 SDK for Unity R3

【OS名・バージョン】  
エディション Windows 10 Home  
バージョン 21H2  
インストール日 ‎2022/‎01/‎23  
OS ビルド 19044.1466  
エクスペリエンス Windows Feature Experience Pack 120.2212.3920.0

【開発使用ツール】  
Visual Studio Community 2017 ver15.6.5  
Unity 2020.3.21f1 Personal

【不具合の状況】  
Unityにインポートしたモデルデータのパーツの透明度を  
スクリプトから制御したいのですが  
「Opacity」を使用してもUnity上で反映されません。  
どうしたらよいでしょうか。

下記の記述では透明度を変化させられませんでした。  
初期状態ではパーツの不透明度は０です。  
Model.Parts[20].Opacity = 1.0f;

# 以下C#スクリプト一部

```
[SerializeField] GameObject L2DM;
public CubismModel Model;

// Start is called before the first frame update
void Start()
{
     L2DM = GameObject.Find("LookIn_model(Ars)");
    Model = L2DM.GetComponent<CubismModel>();
}

// Update is called once per frame
void Update()
{
    
}
public void LateUpdate()
{

}
public void AddSwitch()
{
    Debug.Log("OK");
    Model.Parts[20].Opacity = 1.0f;       
}

```

============================================

こちらの情報を参考にしました。

> **[unity 上でパーツの透明度の設定](https://forum.live2d.com/discussion/comment/2751#Comment_2751)**
>
> unity上でパーツの透明度を設定したいのですが、cubism parts inspectorのパラメー…

---

<div class="post-metadata">

**Author:** ![ono-at-live2d-com](https://sea1.discourse-cdn.com/flex015/user_avatar/creatorsforum.live2d.com/ono-at-live2d-com/32/167_2.png) [@ono-at-live2d-com](https://creatorsforum.live2d.com/u/ono-at-live2d-com)\
**Post date:** [2022 年 2 月 3 日午前 9:41 UTC](https://creatorsforum.live2d.com/t/topic/528/2 "2022-02-03T09:41:37Z")

</div>

@Lexer_Flanagan 様

ご質問誠にありがとうございます。  
Live2Dスタッフでございます。  
いつも弊社製品をご愛顧くださり誠にありがとうございます。

こちらの現象は、おそらくCubismのコンポーネント「CubismParameterStore」によって値が上書きされているためだと思われます。

「CubismParameterStore」は毎フレームのモーション適用直前に、すべてのパラメータやパーツ不透明度の値を直前のフレームのモーション適用直後の状態にリセットするコンポーネントです。  
これは直前のフレームに適用した表情など相対的に値をセットする処理が次のフレームに重ねて適用されないようにするために行っています。  
このリセットするための直前のフレームの値が記録される前に、パーツの不透明度の値を設定することで回避が可能です。

こちらの現象の回避手順は以下のとおりです。

1. 実装していただいたコンポーネントに「[独自のコンポーネントの実行順を制御させる](https://docs.live2d.com/cubism-sdk-tutorials/using-update-controller/)」の手順通り加工する
2. Opacityの値操作をOnLateUpdate()で行うよう修正する
3. コンポーネントの実行順を101～149に設定する

以上、よろしくお願いいたします。

---

<div class="post-metadata">

**Author:** ![Lexer\_Flanagan](https://sea1.discourse-cdn.com/flex015/user_avatar/creatorsforum.live2d.com/lexer_flanagan/32/275_2.png) [@Lexer\_Flanagan](https://creatorsforum.live2d.com/u/Lexer_Flanagan)\
**Post date:** [2022 年 2 月 4 日午前 2:39 UTC](https://creatorsforum.live2d.com/t/topic/528/4 "2022-02-04T02:39:37Z")

</div>

お世話になっております。Lexerです。  
CubismParameterStoreが原因なのですね。  
先程ご提案頂いた方法を試してみました。  
しかしながらまだ上手くいきません。

そもそもModel.Parts[20].Opacity = 1.0f;の数字20の部分は  
CubismEditorのインスペクタで確認できるIDのことでよろしいでしょうか。

また教えて頂きました１番のCubismExampleControllerはC#スクリプトを新規作成し  
UnityのLive2DのPrehabにアタッチする、  
３番のコンポーネントの実行順はUnityEditorのProjectSettingにある  
Script Execution Orderの数値を変更するということで合っていますでしょうか。

 ![SnapCrab_NoName_2022-2-4_11-29-57_No-00](https://us1.discourse-cdn.com/flex015/uploads/live2dtest/original/1X/444bec8414b578f8a3fd0b35d0502ae4bd716991.png)

# 以下C#スクリプト一部

public class CubismExampleController : MonoBehaviour,ICubismUpdatable  
{  
[SerializeField] GameObject L2DM;  
public CubismModel Model;  
// Sceneの非実行中に実行順の制御を行うか  
public bool NeedsUpdateOnEditing  
{  
get { return false; }  
}  
//このコンポーネントの実行順  
//ExecutionOrderは、このコンポーネントの実行順を決める値になります。  
//この値が小さいほど他のコンポーネントよりも先に呼ばれます。  
public int ExecutionOrder  
{  
get { return 150; }  
}  
// 実行順序が制御されるアップデート関数  
public void OnLateUpdate()  
{  
// CubismExampleControllerの更新処理  
Debug.Log(“追加処理中・・・”);  
Model.Parts[20].Opacity = 1.0f;  
}  
// 実行順が制御されているか  
public bool HasUpdateController { get; set; }

```
private void Start()
{
    // CubismExampleControllerの初期化処理
    L2DM = GameObject.Find("LookIn_model(Ars)");
    Model = L2DM.GetComponent<CubismModel>();
    // モデルのPrefabにCubismUpdateControllerがアタッチされているかをチェック
    HasUpdateController = (GetComponent<CubismUpdateController>() != null);
}

private void LateUpdate()
{
    // CubismExampleControllerの更新処理
    // CubismUpdateControllerがアタッチされていない場合、CubismExampleController自身のイベント関数から更新処理を行う
    if (!HasUpdateController)
    {
        OnLateUpdate();
    }
}

```

---

<div class="post-metadata">

**Author:** ![Lexer\_Flanagan](https://sea1.discourse-cdn.com/flex015/user_avatar/creatorsforum.live2d.com/lexer_flanagan/32/275_2.png) [@Lexer\_Flanagan](https://creatorsforum.live2d.com/u/Lexer_Flanagan)\
**Post date:** [2022 年 2 月 7 日午前 4:46 UTC](https://creatorsforum.live2d.com/t/topic/528/5 "2022-02-07T04:46:44Z")

</div>

先程モデルの透明度の変更に成功しました。  
Model.Parts[1].Opacity = 0f;

しかし私は画像のゲームオブジェクトをクリックした際  
Live2Dモデルのあるパーツの不透明度を変化させたいのです。  
そのパーツがモデルデータにおいて何番なのか  
そもそも上記の内の数字がどういう仕組みなのか知りたいです。

リファレンスを読み漁ってもどこにもOpacityについて  
詳しく書いていなくて困っています。  
Partsについてもう少し詳しく教えて頂けないでしょうか。

ちなみに以下のリンク先を参考にモデル全体の透明度を下げることも出来ました。

> **[Live2DモデルをUnity上でスクリプトを使って透明にする方法](https://forum.live2d.com/discussion/1240/live2d%E3%83%A2%E3%83%87%E3%83%AB%E3%82%92unity%E4%B8%8A%E3%81%A7%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E9%80%8F%E6%98%8E%E3%81%AB%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95)**
>
> http://forum.live2d.com/discussion/956/unity-上でパーツの透明度の設定 にて、Cubism Render Controllerから…

以下参考スクリプト

```auto
private CubismRenderController rc;
private void Start()
    {
        // CubismExampleControllerの初期化処理
        rc = GetComponent<CubismRenderController>();
    }
public void OnLateUpdate()
    {
        // CubismExampleControllerの更新処理
        rc.Opacity = 0.5f;
    }

```

---

<div class="post-metadata">

**Author:** ![ono-at-live2d-com](https://sea1.discourse-cdn.com/flex015/user_avatar/creatorsforum.live2d.com/ono-at-live2d-com/32/167_2.png) [@ono-at-live2d-com](https://creatorsforum.live2d.com/u/ono-at-live2d-com)\
**Post date:** [2022 年 2 月 16 日午前 2:56 UTC](https://creatorsforum.live2d.com/t/topic/528/6 "2022-02-16T02:56:00Z")

</div>

@Lexer_Flanagan 様

この度はリファレンスの不備のためご不便をおかけして誠に申し訳ございません。  
こちらの内容につきましてはマニュアルに記載を追加させていただきます。

CubismModel.Partsで取得できる配列の並び順は、[Prefab]/Parts/ 以下にあるGameObjectの並び順と同じなので、こちらでインデックスを確認することは可能です。

 ![2022-02-16_11h46_45](https://us1.discourse-cdn.com/flex015/uploads/live2dtest/original/1X/a04341972ea1c65de7422e903f88a997a72fc53a.png)

しかし、CubismPartにはパーツIDをもつプロパティがございますので、こちらで任意のIDを探して取得することを推奨します。

```auto
    private CubismPart _part;

    private void Start()
    {
        var parts = GetComponent<CubismModel>().Parts;

        // 特定のパーツIDを検索して取得
        for (var i = 0; i < parts.Length; i++)
        {
            if (parts[i].Id != ("PartArmAL"))
            {
                continue;
            }

            _part = parts[i];
            break;
        }
    }

    public void OnLateUpdate()
    {
        _part.Opacity = 1.0f;
    }

```

また、あらかじめ参照を直接コンポーネントに持たせておくことも可能です。

```
// Inspectorに表示させる
public CubismPart Part;

public void OnLateUpdate()
{
    Part.Opacity = 1.0f;
}

```

![2022-02-16_11h46_38](https://us1.discourse-cdn.com/flex015/uploads/live2dtest/original/1X/5b2f7c25eb62fd09fcf71f79370e55b9224768b5.png)

以上、よろしくお願いいたします。

---

<div class="post-metadata">

**Author:** ![Lexer\_Flanagan](https://sea1.discourse-cdn.com/flex015/user_avatar/creatorsforum.live2d.com/lexer_flanagan/32/275_2.png) [@Lexer\_Flanagan](https://creatorsforum.live2d.com/u/Lexer_Flanagan)\
**Post date:** [2022 年 2 月 17 日午後 7:09 UTC](https://creatorsforum.live2d.com/t/topic/528/7 "2022-02-17T19:09:36Z")

</div>

解決しました。ありがとうございます！
