LoginSignup
riririki
@riririki (riki nona)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

【Unity】エラー「Invalid token 'try' in class, struct, or interface member declaration」

解決したいこと

Unityで、ARで撮影したものをRenderTextureに変換するコードでエラーが発生します。

try以降の処理にエラーが出ています。

ほとんどこちらの、撮影データを変換する部分のコピペになります
https://edom18.hateblo.jp/entry/2020/08/23/121008
Unityのバージョンは2021/1/11です。

発生している問題・エラー

スクリーンショット 2022-03-17 9.56.39.png
スクリーンショット 2022-03-17 9.56.44.png

例)

NameError (uninitialized constant World)

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;



public class CaptureXRCamera : MonoBehaviour
{
    [SerializeField] private ARCameraManager _cameraManager = null;
    [SerializeField] private GameObject _target = null;
    [SerializeField] private Texture2D _sampleTexture = null;
    [SerializeField] private Material _transposeMaterial = null;
    [SerializeField] private Text _text = null;

    private Texture2D _texture = null;
    private RenderTexture _previewTexture = null;
    private Renderer _renderer = null;
    private Material _material = null;

    private bool _needsRotate = true;

    private void RefreshCameraFeedTexture()
    {
        // TryGetLatestImageで最新のイメージを取得します。
        // ただし、失敗の可能性があるため、falseが返された場合は無視します。
        if (!_cameraManager.TryAcquireLatestCpuImage(out XRCpuImage cameraImage)) return;

        // 中略

        // デバイスの回転に応じてカメラの情報を変換するための情報を定義します。
        XRCpuImage.Transformation imageTransformation = (Input.deviceOrientation == DeviceOrientation.LandscapeRight)
            ? XRCpuImage.Transformation.MirrorY
            : XRCpuImage.Transformation.MirrorX;

        // カメライメージを取得するためのパラメータを設定します。
        XRCpuImage.ConversionParams conversionParams =
            new XRCpuImage.ConversionParams(cameraImage, TextureFormat.RGBA32, imageTransformation);

        // 生成済みのTexture2D(_texture)のネイティブのデータ配列の参照を得ます。
        NativeArray<byte> rawTextureData = _texture.GetRawTextureData<byte>();

    }

    try
        {
            unsafe
            {
                // 前段で得たNativeArrayのポインタを渡し、直接データを流し込みます。
                cameraImage.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length);
            }
        }

    finally
        {
            cameraImage.Dispose();
        }

        // 取得したデータを適用します。
        _texture.Apply();

        // 後略
    }
}

自分で試したこと

XRの処理の名前が、アップデートで変更されて「could not be found 」エラーが出ていたので変えました

XRCameraImage →XRCpuImage
XRCameraImageConversionParams→XRCpuImage.ConversionParams
CameraImageTransformation→XRCpuImage.Transformation

変更内容の詳細はhttps://docs.unity3d.com/Packages/com.unity.xr.arfoundation@4.0/manual/migration-guide-3.html#xrcameraimage-is-now-xrcpuimage

0

1Answer

Comments

  1. @riririki

    Questioner
    かんっっぜんにこれでした・・・・泣
    こんな初歩的なミスに答えていただきありがとうございます!!!!泣

Your answer might help someone💌