UnityNativeFilePicker icon indicating copy to clipboard operation
UnityNativeFilePicker copied to clipboard

How can I use ConvertExtensionToFileType for custom extensions?

Open hippogamesunity opened this issue 4 years ago • 16 comments

Hi! For example, I have .psp extension in my app. I've also set Custom Types: image

Then I'm using allowedFileTypes.Add(NativeFilePicker.ConvertExtensionToFileType("png")); allowedFileTypes.Add(NativeFilePicker.ConvertExtensionToFileType("jpg")); ... allowedFileTypes.Add(NativeFilePicker.ConvertExtensionToFileType("psp"));

As a result, I can't pick .psp files both on iOS and Android, they are greyed. On Android - it seem's custom types are simply unsupported.

hippogamesunity avatar Jan 14 '21 17:01 hippogamesunity

What is the output of NativeFilePicker.ConvertExtensionToFileType("psp")?

yasirkula avatar Jan 14 '21 17:01 yasirkula

Hello! It returns com.pixelstudio.psp. I've also tried removing all other extensions and using only "com.pixelstudio.psp" directly without ConvertExtensionToFileType. Still unable to pick psp file. Is it correct to use public.image as Conforms To in my case when psp is just a custom binary file that only my app can read? Thank you for your time.

hippogamesunity avatar Jan 15 '21 11:01 hippogamesunity

Does changing public.image to public.data make any difference? If not, please give the Manual Setup a shot: https://github.com/yasirkula/UnityNativeFilePicker/wiki/Manual-Setup-for-iOS

If it still doesn't work, check out the Document Types that is mentioned here: https://github.com/yasirkula/UnityNativeFilePicker/issues/1. Maybe it will resolve the issue, despite its mentioned downside.

yasirkula avatar Jan 15 '21 13:01 yasirkula

At first I want to note that my type is both exported and imported. But if I check "Is Exported" it will only appear in Xcode in Exported Type Identifiers. I think there should be two checkboxes in Custom Types declaration window. As a workaround it seems I have to specify my item twice (as imported and as exported). I've also tried manual setup but it didn't work for me. Снимок экрана 2021-01-15 в 21 59 55 I'll try the suggested workaround (still in progress).

hippogamesunity avatar Jan 15 '21 19:01 hippogamesunity

Unfortunately I was enable to make this functionality work. For unknown reason "additional imported type identifier properties" are empty after Unity build and reset to null after building in Xcode even if I specify them manually. I wonder if you could accept my paid request to prepare and upload dummy Xcode project that will open files with .psp extension, so I could use it as a working example. Thank you!

hippogamesunity avatar Jan 15 '21 19:01 hippogamesunity

I can't because I don't have access to Mac workstation. Even if I did, I don't think I could help you because all I did during my own tests was filling the properties of Window-NativeFilePicker Custom Types. I didn't do anything special.

I'd like to quote "Is Exported"s tooltip:

If this type is owned by your app (i.e. exclusive to your app), set this value to true. If another app generates files of this type and your app is merely importing/modifying them, set this value to false.

So it isn't about app's capability of importing/exporting those files. Don't declare 2 custom types for the same extension. Simply have a single one and enable its Is Exported option. You can also try disabling that option.

yasirkula avatar Jan 15 '21 20:01 yasirkula

I add this code to Info.plist:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array/>
        <key>CFBundleTypeName</key>
        <string>this is CFBundleTypeName</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.DefaultCompany.vrm079.custom</string>
        </array>
    </dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>vrm model file</string>
        <key>UTTypeIconFiles</key>
        <array/>
        <key>UTTypeIdentifier</key>
        <string>com.DefaultCompany.vrm079.custom</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>vrm</string>
            </array>
        </dict>
    </dict>
    <dict/>
</array>

works for me, If you have any problem please @ me.

orangeagain avatar Aug 13 '21 09:08 orangeagain

I fixed it by changing UTIs[I] to Your IdentifierId like "com.yasirkula.nativefilepicker" and it work.

https://github.com/yasirkula/UnityNativeFilePicker/blob/320d444af1241e2f196ef64c87f309494c4e83e9/Plugins/NativeFilePicker/iOS/NativeFilePicker.mm#L187

andysdds avatar Aug 22 '22 09:08 andysdds

Passing "com.yasirkula.nativefilepicker" to NativeFilePicker.PickFile should've done the same thing because allowedFileTypes parameter is passed directly to the native _NativeFilePicker_PickFile function. Did you try passing hardcoded "com.yasirkula.nativefilepicker" (your own UTI) to NativeFilePicker.PickFile function's allowedFileTypes parameter?

yasirkula avatar Aug 22 '22 10:08 yasirkula

Yeah, it work. In your example code, the pdfFileType still show nothing, and the file will grey out, if I don't hardcoded "com.yasirkula.nativefilepicker" (I am using custom file extension)

andysdds avatar Aug 22 '22 10:08 andysdds

In the example code, did you pass the custom file extension to pdfFileType = NativeFilePicker.ConvertExtensionToFileType( "pdf" );?

yasirkula avatar Aug 22 '22 11:08 yasirkula

Yes, I did.

Edit:If I don't hardcode the "com.yasirkula.nativefilepicker" in NativeFilePicker.PickFile , pdfFileType will be empty

andysdds avatar Aug 23 '22 03:08 andysdds

I have the same problem. I am trying to use this to import a BVH file. I have tried orangeagain's method and andysdds's method, but after opening the file picker, the file is grayed out and cannot be selected.

orange3134 avatar Mar 21 '23 11:03 orange3134

Solved, Conforms To needed to be set to "public.data".

orange3134 avatar Mar 21 '23 15:03 orange3134

FYI: if you want to import custom filetypes eg on Android, I found this plugin doing the job: https://github.com/keiwando/nativefileso

marvpaul avatar Mar 27 '23 05:03 marvpaul

One should inspect its license before using it, I'd recommend.

yasirkula avatar Mar 27 '23 06:03 yasirkula