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

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

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

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

Q&A

解決済

1回答

461閲覧

unity andoroidにカメラを映すためのスクリプトについて。

yoshiteru21

総合スコア44

Unity

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

0グッド

0クリップ

投稿2019/07/12 06:42

using

1using System.Collections.Generic; 2using UnityEngine; 3using UnityEngine.UI; 4using UnityEngine.SceneManagement; 5 6public class DeviceCamera : MonoBehaviour 7{ 8 public SpriteRenderer cameraImage; 9 private WebCamTexture webCamTexture; 10 private AspectRatioFitter aspectRatioFitter; 11 private static int state = 1; 12 13 void Awake() 14 { 15 cameraImage = GetComponent<SpriteRenderer>(); 16 aspectRatioFitter = GetComponent<AspectRatioFitter>(); 17 SceneManager.LoadScene("System", LoadSceneMode.Additive); 18 } 19 20 public void Resume() 21 { 22 if (webCamTexture != null) 23 { 24 webCamTexture.Play(); 25 } 26 } 27 28 public void Pause() 29 { 30 if (webCamTexture != null) 31 { 32 webCamTexture.Stop(); 33 } 34 35 } 36 37 void Start() 38 { 39 Camera mainCamera = Camera.main; 40#if false 41 //最初に作った画面のアスペクト比 42 float defaultAspect = Screen.currentResolution.width / Screen.currentResolution.height; 43 44 //実際の画面のアスペクト比 45 float actualAspect = (float)Screen.width / (float)Screen.height; 46 47 //実機とunity画面の比率 48 float ratio = actualAspect / defaultAspect; 49 50 //サイズ調整 51 mainCamera.orthographicSize /= ratio; 52#endif 53 if (webCamTexture == null) 54 { 55 webCamTexture = new WebCamTexture(); 56 } 57 if (Application.platform == RuntimePlatform.IPhonePlayer) 58 { 59 // cameraImage.transform.localScale = new Vector3(1, -1, 1); 60 } 61 Debug.Log("neko : " + SelectUtil.getName()); 62 webCamTexture.Play(); 63 } 64 65 private void Update() 66 { 67 TextureChange(); 68 69 if (webCamTexture?.isPlaying ?? false) 70 { 71 // transform.localEulerAngles = new Vector3(0, 0, -1 * (float)webCamTexture.videoRotationAngle); 72 // aspectRatioFitter.aspectRatio = (float)webCamTexture.width / (float)webCamTexture.height; 73 74 } 75 } 76 77 public void TextureChange() 78 { 79#if false 80 float width = 720; 81 float height = 1440; 82 float ratio = width / height; 83 float reko = webCamTexture.height * ratio; 84 float riko = height / webCamTexture.height; 85 // Texture -> Spriteに変換する 86 Texture2D texture = CameraStart2.ToTexture2D( webCamTexture); 87 Sprite texture_sprite = Sprite.Create(texture, new Rect((webCamTexture.width / 2) - reko / 2, 0, reko, webCamTexture.height), Vector2.zero); 88 // Sprite texture_sprite = Sprite.Create(texture, new Rect(0, 0, 620, 480), Vector2.zero); 89 cameraImage.sprite = texture_sprite; 90 cameraImage.transform.position = new Vector3((-reko)/100.0f, -(webCamTexture.height)/100.0f, 0); 91 cameraImage.transform.localScale = new Vector3(riko, riko, 1); 92 Debug.Log("camTexture width : " + webCamTexture.width + " riko:"+riko); 93#else 94 95 float width = 720; 96 float height = 1440; 97 float ratio = width / height; 98 float reko = webCamTexture.width * ratio; 99 float riko = height / webCamTexture.height; 100 // Texture -> Spriteに変換する 101 Texture2D texture = CameraStart2.ToTexture2D(webCamTexture); 102 Sprite texture_sprite = Sprite.Create(texture, new Rect((webCamTexture.width / 2 ) - reko / 2, 0, reko, (webCamTexture.height ) - reko / 2), new Vector2(0.5f, 0.5f)); 103 cameraImage.sprite = texture_sprite; 104 cameraImage.transform.localScale = new Vector3(riko, riko, 1); 105 cameraImage.transform.rotation = Quaternion.Euler(0, 0, -90); 106 Debug.Log("camTexture width : " + webCamTexture.width + " riko:" + riko); 107 108#if false 109 //camera.mainを変数に格納 110 Camera mainCamera = Camera.main; 111 //最初に作った画面のアスペクト比 112 float defaultAspect = width / height; 113 //実際の画面のアスペクト比 114 float actualAspect = (float)Screen.width / (float)Screen.height; 115 //実機とunity画面の比率 116 float rerio = actualAspect / defaultAspect; 117 //サイズ調整 118 mainCamera.orthographicSize /= rerio; 119#endif 120 121#endif 122 // cameraImage.material.color = Color.white; 123 // cameraImage.material.mainTexture = webCamTexture; 124 // cameraImage.material.color = Color.white; 125 126 127 } 128} 129コード

andoroidに移植してカメラを映し出すスクリプトを組みました。
かなり重く、落ちてしまいます。
どうすれば軽くなるのかご教授いただけないでしょうか?
きたないスクリプトですいません。

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

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

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

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

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

guest

回答1

0

ベストアンサー

おそらく、TextureChange をUpdate内にいれているからだと思います
Quadに貼り付けてやれば、Spriteに変換しなくていいはずです

投稿2019/07/12 06:57

izmktr

総合スコア2856

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問