1

TLDR: How many different ways can I run a Python program in Unity on an Android device?

This is for a Android app that stores a geolocation, which is to be queried later on other devices.

I know about IronPython but it is not updated for Python 3+, which is what I used to write the Python program I need to run.

More backstory: I am trying to use AWS dynamoDB which has an SDK for Unity as it's built on .net. However, specifically for query search, they have not implemented < or > or compound searches, whereas that implementation is available in Python.

The following Python script works.

import boto3
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr
import sys

lat = sys.argv[1]
lon = sys.argv[2]
r = sys.argv[3]


# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('tree')

fe = Key('lon').between((decimal.Decimal(lat) - decimal.Decimal(r)), (decimal.Decimal(lat) + decimal.Decimal(r)))
 #THIS IS THE BETWEEN SEARCH THAT IS NOT AVAILABLE IN UNITY
fe1 = Key('lat').between((decimal.Decimal(lon) - decimal.Decimal(r)), (decimal.Decimal(lon) + decimal.Decimal(r)))
pe = "#l, lon, treeStage"
# Expression Attribute Names for Projection Expression only.
ean = { "#l": "lat", }
esk = None


response = table.scan(
    FilterExpression=fe,
    ProjectionExpression=pe,
    ExpressionAttributeNames=ean
    )

for i in response['Items']:
    print(json.dumps(i, cls=DecimalEncoder))

while 'LastEvaluatedKey' in response:
    response = table.scan(
        ProjectionExpression=pe,
        FilterExpression=fe and fe1, #THIS IS THE COMPOUND SEARCH THAT DOESNT WORK IN UNITY
        ExpressionAttributeNames= ean,
        ExclusiveStartKey=response['LastEvaluatedKey']
        
        )

    for i in response['Items']:
        print(json.dumps(i, cls=DecimalEncoder))

My question is: what available options do I have to build this Python program into a file type that could be run on an Android device through Unity's C#?

Currently I'm just printing the info received from dynamoDB, but I would be open to reading it directly or writing to a file to read in Unity if necessary.

1

2 Answers 2

1

There no performant and maintained solution to do this. There are some community-made solutions but they are too unstable for a production game.

You can host this python script on a webservices. You can call webserives, it will get data and process it then returns to you. This way is probably the easiest and most efficient.

If not possible, you can use python-for-android. But this complicates things alot and not performant at all.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your comment, I'll look into hosting the script as a webservice. That makes the most sense to me.
1

Yes, you can use python language to program in unity.

Check this out → http://forum.unity3d.com/threads...

Alternatively, you can use this Python Interpreter but its reviews are not that great.

Also, you can call Python from C# using IronPython → Running IronPython Scripts from a C# 4.0 Program

But I would NOT propose to use Python...

Unity's primary development language is C#. I’ll recommend you to learn C#. Read more here → If I want to develop in Unity, should I learn basics of C# or start developing right away?

Or If you are familiar and good at Open GL and Open CV → refer to this Augmented reality with Python and OpenCV (part 1) to create AR app using Python. or use Pygame AR example.

1 Comment

(that link is dead but in my research I found the same post) so the problem with this is that I have a functioning game except for doing scan requests in unity, and it doesn't have the functionality I need, so I'm basically forced to use python or another alternative for doing so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.