Version: 5.4
에셋 번들에 바이너리 데이터 저장 및 로드
에셋 번들에 스크립트 포함

콘텐츠 보호

에셋 전송 중에 암호화를 사용하여 에셋을 보호할 수 있지만, 데이터가 클라이언트에 전달된 후에 콘텐츠를 클라이언트에서 가져오는 방법을 찾을 수도 있습니다. 시중에는 예를 들어 3D 데이터를 드라이버 레벨에서 기록하는 툴이 있는데, 사용자는 이 툴을 사용해 GPU로 전송 중인 모델과 텍스처를 추출할 수 있습니다. 이러한 이유로 Unity는 사용자가 에셋을 추출하고자 마음 먹었다면, 이를 실현할 수 있다는 입장입니다.

하지만 원할 경우 에셋 번들 파일에 자체적인 데이터 암호화를 사용할 수 있습니다.

이렇게 하려면 TextAsset 타입을 이용해 데이터를 바이트로 저장하는 방법을 통해 데이터 파일을 암호화하고 .bytes 확장자로 저장할 수 있습니다. 이 파일은 Unity에서 TextAsset 타입으로 처리됩니다. 에디터에서 파일이 TextAsset으로 임포트되면 에셋 번들에 포함되어 서버에 저장될 수 있습니다. 클라이언트 쪽에서는 에셋 번들이 다운로드되고 TextAsset에 저장된 바이트에서 콘텐츠 암호가 해독됩니다. 이 메서드를 사용하면 에셋 번들이 암호화되지 않지만 TextAsset으로 저장된 데이터는 암호화됩니다.

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
    while (!Caching.ready)
        yield return null;

    // Start a download of the encrypted assetbundle
    WWW www = new WWW.LoadFromCacheOrDownload (url, 1);

    // Wait for download to complete
    yield return www;

    // Load the TextAsset from the AssetBundle
    TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset));
 
    // Get the byte data
    byte[] encryptedData = textAsset.bytes;

    // Decrypt the AssetBundle data
    byte[] decryptedData = YourDecryptionMethod(encryptedData);

    // Use your byte array. The AssetBundle will be cached
}

에셋 번들을 소스에서 완전히 암호화하고 WWW 클래스를 사용하여 다운로드하는 방법을 대신 사용할 수도 있습니다. 서버가 번들을 바이너리 데이터로 제공하는 한, 파일 확장자를 마음대로 지정할 수 있습니다. 번들이 다운로드된 후에는 WWW 인스턴스의 .bytes 프로퍼티에서 데이터에 암호화 해제 루틴을 사용해 암호가 해독된 에셋 번들 파일 데이터를 가져오고 AssetBundle.CreateFromMemory를 사용해 에셋 번들을 메모리에서 만들 수 있습니다.

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
    // Start a download of the encrypted assetbundle
    WWW www = new WWW (url);

    // Wait for download to complete
    yield return www;

    // Get the byte data
    byte[] encryptedData = www.bytes;

    // Decrypt the AssetBundle data
    byte[] decryptedData = YourDecryptionMethod(encryptedData);

    // Create an AssetBundle from the bytes array

    AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);
    yield return acr;

    AssetBundle bundle = acr.assetBundle;

    // You can now use your AssetBundle. The AssetBundle is not cached.
}

후자의 경우 전자에 비해 (AssetBundles.LoadFromCacheOrDownload를 제외한) 메서드를 사용하여 바이트를 전송할 수 있고, 플러그인의 소켓처럼 데이터가 완전히 암호화되는 등의 장점이 있습니다. 하지만 Unity의 자동 캐싱을 사용하여 캐싱되지 않는다는 단점도 있습니다. 모든 플레이어에서 AssetBundles.CreateFromFile을 사용해 파일을 수동으로 디스크에 저장하고 로드할 수 있습니다.

앞에서 설명한 두 가지 방법의 장점만 결합하여 에셋 번들 자체를 TextAsset으로 다른 일반 에셋 번들에 저장하는 세 번째 방법도 있습니다. 암호화된 에셋 번들이 포함된 암호화되지 않은 에셋 번들은 캐싱됩니다. 그러면 원본 에셋 번들이 AssetBundle.CreateFromMemory을 사용하여 메모리에 로드되고 암호가 해독되고 인스턴스화됩니다.

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
    while (!Caching.ready)
        yield return null;

    // Start a download of the encrypted assetbundle
    WWW www = new WWW.LoadFromCacheOrDownload (url, 1);

    // Wait for download to complete
    yield return www;

    // Load the TextAsset from the AssetBundle
    TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset));
 
    // Get the byte data
    byte[] encryptedData = textAsset.bytes;

    // Decrypt the AssetBundle data
    byte[] decryptedData = YourDecryptionMethod(encryptedData);

    // Create an AssetBundle from the bytes array
    AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);
    yield return acr;

    AssetBundle bundle = acr.assetBundle;

    // You can now use your AssetBundle. The wrapper AssetBundle is cached
}

에셋 번들에 바이너리 데이터 저장 및 로드
에셋 번들에 스크립트 포함