質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

1728閲覧

canvasが操作できない some values driven by canvas

nounasiHuman

総合スコア13

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2022/09/03 18:34

イメージ説明### 前提

https://tedenglish.site/unity-modify-anyresolution/
を模倣してどのスマホでも同じ解像度にし、ビルドをスマホにしました。サイズが合わなかったのでアスペクト比を変更しようと思いCnvaseを見るとsome values driven by canvasと表示されCnvaseが動かなくなってしました。元々作成したプロジェクトも新規作成したプロジェクトも全てsome values driven by canvasと表示されてしまいます。

どうしたら元に戻すことができるのでしょうか

発生している問題・エラーメッセージ

some values driven by canvas

該当のソースコード

//カメラのスクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode()]
[RequireComponent(typeof(Camera))]
public class CameraStableAspect : MonoBehaviour
{
[SerializeField]
Camera refCamera;

[SerializeField] int width = 1920; [SerializeField] int height = 1080; [SerializeField] float pixelPerUnit = 100f; int m_width = -1; int m_height = -1; void Awake() { if( refCamera == null ){ refCamera = GetComponent< Camera >(); } UpdateCamera(); } private void Update() { UpdateCameraWithCheck(); } void UpdateCameraWithCheck() { if(m_width == Screen.width && m_height == Screen.height){ return; } UpdateCamera(); } void UpdateCamera() { float screen_w = (float)Screen.width; float screen_h = (float)Screen.height; float target_w = (float)width; float target_h = (float)height; //アスペクト比 float aspect = screen_w / screen_h; float targetAcpect = target_w / target_h; float orthographicSize = (target_h / 2f / pixelPerUnit); //縦に長い if (aspect < targetAcpect) { float bgScale_w = target_w / screen_w; float camHeight = target_h / (screen_h * z); refCamera.rect = new Rect( 0f, (1f-camHeight)*0.5f, 1f, camHeight); } // 横に長い else { // カメラのorthographicSizeを横の長さに合わせて設定しなおす float bgScale = aspect / targetAcpect; orthographicSize *= bgScale; float bgScale_h = target_h / screen_h; float camWidth = target_w / (screen_w * bgScale_h); refCamera.rect = new Rect( (1f-camWidth)*0.5f, 0f, camWidth, 1f); } refCamera.orthographicSize = orthographicSize; m_width = Screen.width; m_height = Screen.height;

//Cnvaseのスクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode()]
public class RectScalerWithViewport : MonoBehaviour
{
[SerializeField]
RectTransform refRect = null;

[SerializeField] Vector2 referenceResolution = new Vector2(1920, 1080); [Range(0, 1)] [SerializeField] float matchWidthOrHeight = 0; float m_width = -1; float m_height = -1; float m_matchWidthOrHeight = 0f; private const float kLogBase = 2; private void Awake() { if(refRect == null){ refRect = GetComponent<RectTransform>(); } UpdateRect(); } private void Update() { UpdateRectWithCheck(); } private void OnValidate() { UpdateRect(); } void UpdateRectWithCheck() { Camera cam = Camera.main; float width = cam.rect.width * Screen.width; float height = cam.rect.height * Screen.height; if(m_width == width && m_height == height && m_matchWidthOrHeight == matchWidthOrHeight ){ return; } UpdateRect(); } void UpdateRect() { if( referenceResolution.x == 0f || referenceResolution.y == 0f){ return; } Camera cam = Camera.main; if( cam == null ){ return; } float width = cam.rect.width * Screen.width; float height = cam.rect.height * Screen.height; if( width == 0f || height == 0f ){ return; } // canvas scalerから引用 float logWidth = Mathf.Log(width / referenceResolution.x, kLogBase); float logHeight = Mathf.Log(height / referenceResolution.y, kLogBase); float logWeightedAverage = Mathf.Lerp(logWidth, logHeight, matchWidthOrHeight); float scale = Mathf.Pow(kLogBase, logWeightedAverage); if( float.IsNaN(scale) || scale <= 0f ){ return; } refRect.localScale = new Vector3(scale, scale, scale); // スケールで縮まるので領域だけ広げる float revScale = 1f / scale; refRect.sizeDelta = new Vector2(width * revScale, height * revScale); m_width = width; m_height = height; m_matchWidthOrHeight = matchWidthOrHeight;

}
}

試したこと

UnityHub再インストール

補足情報(FW/ツールのバージョンなど)

2020.1.2f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

エラー内容で検索を掛けてみましたか?
同じ症状の解決方法が沢山出てきましたよ。
以下は知恵袋の回答からの引用です。

CanvasコンポーネントのRender ModeをWorld Spaceにしてください。
これ以外だと、Screen Space - Overlayの場合は画面の解像度とCanvas右上の座標を一致させるため、Screen Space - Cameraの場合は指定したカメラのサイズにCanvasスクリプトが自動的に合わせるため、変更できなくなっています。

投稿2022/09/05 05:27

Y0241-N

総合スコア1066

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問